Sponsored Content
Top Forums Programming Python variable at different position Post 303042751 by sahil_shine on Sunday 5th of January 2020 09:44:22 PM
Old 01-05-2020
Python variable at different position

I have a file which records banking transactions say like below.

Code:
user1 has deposited 10,000$ in his account
user2 has deposited 11,000$ in his account
user1 has withdraw today 5000$ from his account.


Lets say i read this file and convert each line as a list.

Code:
username= word[0]
action= word[2]
amount =word[3] or word[4]

If you see above lines amount is at [3] for deposit and [4] for withdraw. How can i write a function for lets say final balance after all this transactions i.e deposit and withdraw.
Problem i am looking for answer is how variable can be assigned value based on condition.

Moderator's Comments:
Mod Comment Please do wrap your samples/codes in CODE TAGS as per forum rules.

Last edited by RavinderSingh13; 01-05-2020 at 11:40 PM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cut position as a variable

Hi All, I wish to cut an input by using the below command but i would want to have the cut position as a variable. For eg, the position number is 11 now and i wish that this number is being assigned to a variable before putting into the cut function. Can this be done ? Can any expert help?... (1 Reply)
Discussion started by: Raynon
1 Replies

2. Shell Programming and Scripting

Shell to Python variable passing

Hi, I had to create a new thread as the old thread had to much of confusion I have two files shashi.sh and py.py I want to pass a variable from shashi.sh to py.py. How do i achieve that ?. shashi.sh export X=12 echo "$("pwd")" echo "$X" exec python py.py "$(X)" py.py... (0 Replies)
Discussion started by: shashi792
0 Replies

3. Shell Programming and Scripting

Get character position within variable

Hi all let's say i have a file named 1234_v2_abcd i need to find the position of the character located after _v, in above case this would be character number 2 the count of the characters before _v can change, but i always have a number after _v can anybody help :) (4 Replies)
Discussion started by: gigagigosu
4 Replies

4. Shell Programming and Scripting

Finding position of space in a variable

HI All, am trying to find the position of space in a variable, it is working for other characters other than space ulab="ulab1|ulab2" find_pos=`expr index $ulab '|'` echo $find_pos above code worked fine but below one says syntax error ulab="ulab ulab2" find_pos=`expr index $ulab ' '`... (2 Replies)
Discussion started by: ulab
2 Replies

5. Shell Programming and Scripting

Change text at a variable position

Hi there script guru's I have an input file /tmp/in.txt of which the data is seperated by a : (example of the data) test1:zz:2000:2000:zzz te:a:2000:3333:bbb testabs:x:2004:3000:cccc I would like to run a scrip (bash) changing the data after the second ":" example Test for the value of... (3 Replies)
Discussion started by: KobusE
3 Replies

6. UNIX for Dummies Questions & Answers

Help with awk, where line length and field position are variable

I have several questions about using awk. I'm hoping someone could lend me a hand. (I'm also hoping that my questions make sense.) I have a file that contains pipe separated data. Each line has similar data but the number of fields and the field position on each line is variable. ... (3 Replies)
Discussion started by: Cheese64
3 Replies

7. Shell Programming and Scripting

Python get the expect value from a variable

the value of the variable is yes group=bsp_16 keyword="82599" test_var="-a xxx -b yyy" I want to get output with -a xxx -b yyy (0 Replies)
Discussion started by: yanglei_fage
0 Replies

8. Shell Programming and Scripting

Variable Substitution in Python

Hi, Please I have the following python code. x = time.strftime("%Y_%m_%d") os.system("/gsn/mme/parse_ebm_log.pl -x /gsn/mme/ -u -r gsm -f /gsn/mme/mme01/ebs/A* >> /gsn/mme/mme01/+ str(x)_gsm.txt") os.system("/gsn/mme/parse_ebm_log.pl -x /gsn/mme/ -u -r wcdma -f /gsn/mme/mme01/ebs/A* >>... (2 Replies)
Discussion started by: infinitydon
2 Replies

9. Shell Programming and Scripting

Pass bash variable to python

How can I pass bash Variable to python script. bash.sh while read -r db do Printf "%s\n" ${db} "Found" done < path/to/file.txt file.txt db1 db2 db3 python.py print(${db}_tables.replicate.fix) (2 Replies)
Discussion started by: lpoolfc
2 Replies

10. UNIX for Beginners Questions & Answers

How to insert a string and variable at specified position in command in bash?

I currently have a loop that reads all .bam files in a directory (wont always be 4 like in this example, into $id. What I am trying to do, unsucessfully, is create specific new lines in an exsisting command using each $id. Each new line would be: --bam ${id} \ Tried p=$dir... (8 Replies)
Discussion started by: cmccabe
8 Replies
Moose::Cookbook::Basics::BankAccount_MethodModifiersAndSUseraContributed PeMoose::Cookbook::Basics::BankAccount_MethodModifiersAndSubclassing(3pm)

NAME
Moose::Cookbook::Basics::BankAccount_MethodModifiersAndSubclassing - Demonstrates the use of method modifiers in a subclass VERSION
version 2.0603 SYNOPSIS
package BankAccount; use Moose; has 'balance' => ( isa => 'Int', is => 'rw', default => 0 ); sub deposit { my ( $self, $amount ) = @_; $self->balance( $self->balance + $amount ); } sub withdraw { my ( $self, $amount ) = @_; my $current_balance = $self->balance(); ( $current_balance >= $amount ) || confess "Account overdrawn"; $self->balance( $current_balance - $amount ); } package CheckingAccount; use Moose; extends 'BankAccount'; has 'overdraft_account' => ( isa => 'BankAccount', is => 'rw' ); before 'withdraw' => sub { my ( $self, $amount ) = @_; my $overdraft_amount = $amount - $self->balance(); if ( $self->overdraft_account && $overdraft_amount > 0 ) { $self->overdraft_account->withdraw($overdraft_amount); $self->deposit($overdraft_amount); } }; DESCRIPTION
The first recipe demonstrated how to build very basic Moose classes, focusing on creating and manipulating attributes. The objects in that recipe were very data-oriented, and did not have much in the way of behavior (i.e. methods). In this recipe, we expand upon the concepts from the first recipe to include some real behavior. In particular, we show how you can use a method modifier to implement new behavior for a method. The classes in the SYNOPSIS show two kinds of bank account. A simple bank account has one attribute, the balance, and two behaviors, depositing and withdrawing money. We then extend the basic bank account in the CheckingAccount class. This class adds another attribute, an overdraft account. It also adds overdraft protection to the withdraw method. If you try to withdraw more than you have, the checking account attempts to reconcile the difference by withdrawing money from the overdraft account.(1) The first class, BankAccount, introduces a new attribute feature, a default value: has 'balance' => ( isa => 'Int', is => 'rw', default => 0 ); This says that a BankAccount has a "balance" attribute, which has an "Int" type constraint, a read/write accessor, and a default value of 0. This means that every instance of BankAccount that is created will have its "balance" slot initialized to 0, unless some other value is provided to the constructor. The "deposit" and "withdraw" methods should be fairly self-explanatory, as they are just plain old Perl 5 OO.(2) As you know from the first recipe, the keyword "extends" sets a class's superclass. Here we see that CheckingAccount "extends" BankAccount. The next line introduces yet another new attribute feature, class-based type constraints: has 'overdraft_account' => ( isa => 'BankAccount', is => 'rw' ); Up until now, we have only seen the "Int" type constraint, which (as we saw in the first recipe) is a builtin type constraint. The "BankAccount" type constraint is new, and was actually defined the moment we created the BankAccount class itself. In fact, Moose creates a corresponding type constraint for every class in your program(3). This means that in the first recipe, constraints for both "Point" and "Point3D" were created. In this recipe, both "BankAccount" and "CheckingAccount" type constraints are created automatically. Moose does this as a convenience so that your classes and type constraint can be kept in sync with one another. In short, Moose makes sure that it will just DWIM(4). In CheckingAccount, we see another method modifier, the "before" modifier. before 'withdraw' => sub { my ( $self, $amount ) = @_; my $overdraft_amount = $amount - $self->balance(); if ( $self->overdraft_account && $overdraft_amount > 0 ) { $self->overdraft_account->withdraw($overdraft_amount); $self->deposit($overdraft_amount); } }; Just as with the "after" modifier from the first recipe, Moose will handle calling the superclass method (in this case "BankAccount->withdraw"). The "before" modifier will (obviously) run before the code from the superclass is run. Here, "before" modifier implements overdraft protection by first checking if there are available funds in the checking account. If not (and if there is an overdraft account available), it transfers the amount needed into the checking account(5). As with the method modifier in the first recipe, we could use "SUPER::" to get the same effect: sub withdraw { my ( $self, $amount ) = @_; my $overdraft_amount = $amount - $self->balance(); if ( $self->overdraft_account && $overdraft_amount > 0 ) { $self->overdraft_account->withdraw($overdraft_amount); $self->deposit($overdraft_amount); } $self->SUPER::withdraw($amount); } The benefit of taking the method modifier approach is we do not need to remember to call "SUPER::withdraw" and pass it the $amount argument when writing "CheckingAccount->withdraw". This is actually more than just a convenience for forgetful programmers. Using method modifiers helps isolate subclasses from changes in the superclasses. For instance, if BankAccount->withdraw were to add an additional argument of some kind, the version of CheckingAccount->withdraw which uses "SUPER::withdraw" would not pass that extra argument correctly, whereas the method modifier version would automatically pass along all arguments correctly. Just as with the first recipe, object instantiation uses the "new" method, which accepts named parameters. my $savings_account = BankAccount->new( balance => 250 ); my $checking_account = CheckingAccount->new( balance => 100, overdraft_account => $savings_account, ); And as with the first recipe, a more in-depth example can be found in the t/recipes/moose_cookbook_basics_recipe2.t test file. CONCLUSION
This recipe expanded on the basic concepts from the first recipe with a more "real world" use case. FOOTNOTES(1) If you're paying close attention, you might realize that there's a circular loop waiting to happen here. A smarter example would have to make sure that we don't accidentally create a loop between the checking account and its overdraft account.(2) Note that for simple methods like these, which just manipulate some single piece of data, it is often not necessary to write them at all. For instance, "deposit" could be implemented via the "inc" native delegation for counters - see Moose::Meta::Attribute::Native::Trait::Counter for more specifics, and Moose::Meta::Attribute::Native for a broader overview.(3) In reality, this creation is sensitive to the order in which modules are loaded. In more complicated cases, you may find that you need to explicitly declare a class type before the corresponding class is loaded.(4) Moose does not attempt to encode a class's is-a relationships within the type constraint hierarchy. Instead, Moose just considers the class type constraint to be a subtype of "Object", and specializes the constraint check to allow for subclasses. This means that an instance of CheckingAccount will pass a "BankAccount" type constraint successfully. For more details, please refer to the Moose::Util::TypeConstraints documentation.(5) If the overdraft account does not have the amount needed, it will throw an error. Of course, the overdraft account could also have overdraft protection. See note 1. ACKNOWLEDGMENT
The BankAccount example in this recipe is directly taken from the examples in this chapter of "Practical Common Lisp": http://www.gigamonkeys.com/book/object-reorientation-generic-functions.html <http://www.gigamonkeys.com/book/object-reorientation-generic- functions.html> AUTHOR
Moose is maintained by the Moose Cabal, along with the help of many contributors. See "CABAL" in Moose and "CONTRIBUTORS" in Moose for details. COPYRIGHT AND LICENSE
This software is copyright (c) 2012 by Infinity Interactive, Inc.. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. perl v5.14.2 2012-06Moose::Cookbook::Basics::BankAccount_MethodModifiersAndSubclassing(3pm)
All times are GMT -4. The time now is 12:57 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy