Re: Is there a simpler way to modify all arguments in a function before using the arguments?
On Saturday, November 10, 2012 10:35:12 AM UTC-5, Aahz wrote: > In article , > > Peter Otten <__pete...@web.de> wrote: > > >Miki Tebeka wrote: > > > > > >>> Is there a simpler way to modify all arguments in a function before using > > >>> the arguments? > > >> > > >> You can use a decorator: > > >> > > >> from functools import wraps > > >> > > >> def fix_args(fn): > > >> @wraps(fn) > > >> def wrapper(*args): > > >> args = (arg.replace('_', '') for arg in args) > > >> return fn(*args) > > >> > > >> return wrapper > > >> > > >> @fix_args > > >> def foo(x, y): > > >> print(x) > > >> print(y) > > > > > >I was tempted to post that myself, but he said /simpler/ ;) > > > > From my POV, that *is* simpler. When you change the parameters for foo, > > you don't need to change the arg pre-processing. Also allows code reuse, > > probably any program needing this kind of processing once will need it > > again. > > -- > > Aahz (a...@pythoncraft.com) <*> http://www.pythoncraft.com/ > > > > "Normal is what cuts off your sixth finger and your tail..." --Siobhan Using a decorator works when named arguments are not used. When named arguments are used, unexpected keyword error is reported. Is there a simple fix? Thanks to all, Bruce Code: - from functools import wraps def fix_args(fn): @wraps(fn) def wrapper(*args): args = (arg.replace('_', '') for arg in args) return fn(*args) return wrapper @fix_args def foo(a1="", a2="", b1="", b2=""): print(a1) print(a2) print(b1) print(b2) foo ('a1a1_x', 'a2a2_x', 'b1b1_x', 'b2b2_x') foo (a1='a1a1_x', a2='a2a2_x', b1='b1b1_x', b2='b2b2_x') Results: a1a1x a2a2x b1b1x b2b2x Traceback (most recent call last): File "C:\WORK\masterDB_Update\argtest.py", line 19, in foo (a1='a1a1_x', a2='a2a2_x', b1='b1b1_x', b2='b2b2_x') TypeError: wrapper() got an unexpected keyword argument 'a1' -- http://mail.python.org/mailman/listinfo/python-list
Re: best way to remove leading zeros from a tuple like string
On Monday, May 21, 2018 at 1:05:52 PM UTC-4, Rodrigo Bistolfi wrote: > >>> repr(tuple(int(i) for i in s[1:-1].split(','))) > '(128, 20, 8, 255, -1203, 1, 0, -123)' > > 2018-05-21 4:26 GMT-03:00 Peter Otten <__pete...@web.de>: > > > bruceg113...@gmail.com wrote: > > > > > Looking over the responses, I modified my original code as follows: > > > > > s = "(128, 020, 008, 255, -1203,01,-000, -0123)" > > ",".join([str(int(i)) for i in s[1:-1].split(",")]) > > > '128,20,8,255,-1203,1,0,-123' > > > > I think this looks better with a generator instead of the listcomp: > > > > >>> ",".join(str(int(i)) for i in s[1:-1].split(",")) > > '128,20,8,255,-1203,1,0,-123' > > > > > > -- > > https://mail.python.org/mailman/listinfo/python-list > > I am not familiar with the repr statement. Looking into your response, I like it. There is no need to add-in the parentheses. With the repr statment: >>> bb = repr (tuple (int (i) for i in s [1: -1] .split (','))) >>> bb '(128, 20, 8, 255, -1203, 1, 0, -123)' >>> type(bb) >>> Without the repr statement: >>> aa = (tuple (int (i) for i in s [1: -1] .split (','))) >>> aa (128, 20, 8, 255, -1203, 1, 0, -123) >>> type(aa) >>> Thanks, Bruce -- https://mail.python.org/mailman/listinfo/python-list
Re: convert a string to a variable
On Friday, May 25, 2018 at 1:56:14 AM UTC-4, dieter wrote: > bruceg113...@gmail.com writes: > > > I am trying to convert a string to a variable. > > > > I got cases 1 & 2 to work, but not cases 3 & 4. > > > > The print statement in cases 3 & 4 reports the following: > > builtins.AttributeError: type object 'animal' has no attribute 'tiger' > > > > I am stuck on creating variables that can be accessed as follows. > > animal.tiger > > self.animal.tiger > > > > Any suggestions? > > ... > > # Case 3: This does not work > > indata = 'animal.tiger' > > vars()[indata] = "Tigers, big and strong!" > > print (animal.tiger) > > In the expression "animal.tiger", the "variable" is "animal", > not "animal.tiger". It is evaluated as follows: > determine the object bound to "animal", access its attribute "tiger". > Your error message tells you that the first step (object bound > to "animal") has been successful, but the result lacks the > attribute "tiger". > > > #Case 4: This does not work > > class animal(): > > def create (self, indata): > > vars(self)[indata] = "Tigers, big and strong!" > > Here you want to define the attribute "tiger" (I think), > not "animal.tiger". Note that the "." in a Python expression > (not a string) separates two individual steps: determine > an object corresponding to the leftside to the "."; access > the attribute corresponding to the name following the ".". > > print (self.animal.tiger) > > > > tmp = animal() > > tmp.create('animal.tiger') Hi Dieter, After reading your response, I picked up on the 'attribute' word. Updated case 3: (Success) indata = 'tiger' setattr (animal, indata, "Tigers, big and strong!!!") print (animal.tiger) animal.tiger = "Lions are bigger and stronger" print (animal.tiger) Output is: Tigers, big and strong!!! Lions are bigger and stronger I am still working on case 4 using setattr. Thanks, Bruce -- https://mail.python.org/mailman/listinfo/python-list
Re: convert a string to a variable
On Friday, May 25, 2018 at 8:06:31 AM UTC-4, Ned Batchelder wrote: > On 5/24/18 6:54 PM, bruceg113...@gmail.com wrote: > > I am trying to convert a string to a variable. > > > > I got cases 1 & 2 to work, but not cases 3 & 4. > > > > The print statement in cases 3 & 4 reports the following: > > builtins.AttributeError: type object 'animal' has no attribute 'tiger' > > > > I am stuck on creating variables that can be accessed as follows. > >animal.tiger > >self.animal.tiger > > > > Any suggestions? > > > > > > Usually when people want to turn strings into variables, the best answer > is to not make variables, but instead have a dictionary. > > But I don't know what you are going to do with "animal.tiger", so I'm > not sure the best answer. Can you say more about the whole problem? > > --Ned. Hi Ned, I am writing a small interpreter just for fun. My program reads and processes text from a file. Good or bad, I prefer the variable syntax. I got animal.tiger syntax to work. (thanks Dieter) I am still trying to get self.animal.tiger syntax to work. Thanks, Bruce -- https://mail.python.org/mailman/listinfo/python-list