Reputation: 35149
I have this method:
def get_user_info(self):
month = choice(range(1,13))
day = choice(range(1,29))
year = choice(range(1966,1994))
f_name = self.assign_name('FirstNames')
l_name = self.assign_name('LastNames')
e_address = f_name+l_name+year.__str__()+day.__str__()
password = f_name+l_name+'0008383'
user_info = dict({
'month' : month,
'day' : day,
'year' : year,
'f_name' : f_name,
'l_name' : l_name,
'e_address' : e-address,
'password' : password
})
print user_info
It gives me the right result, But I fill like I'm writing the same code twice. So my question is how to get the same result without first creating variables, and than put them into dict().
Note, I need to use f_name and l_name and so on in different key:value pairs.
Upvotes: 0
Views: 237
Reputation: 730
This is probably the shortest you can do without sacrificing readability (but improving on PEP8 compilance, for instance):
def get_user_info(self):
month = choice(range(1, 13))
day = choice(range(1, 29))
year = choice(range(1966, 1994))
f_name = self.assign_name('FirstNames')
l_name = self.assign_name('LastNames')
print {'month' : month,
'day' : day,
'year' : year,
'f_name' : f_name,
'l_name' : l_name,
'e_address' : f_name + l_name + str(year) + str(day),
'password' : f_name + l_name + '0008383'}
As a side note, do reconsider your password generation policy, it's quite insecure in your code.
Also did you notice you've probably missed out month
from e_address
assignment?
Upvotes: 0
Reputation: 104792
I'm inclined to think that, given the values you're computing, the way you're doing it now is more or less the best you can do. There are some small improvements, like skipping your call to dict
and using the str
built-in function rather than calling __str__
methods directly, but there aren't really any good changes to make at a high level.
The reason is that you're using each of the first five of your variables at least twice. You use them once individually, as a value that's stored in the dictionary under their own key (e.g. the f_name
value is stored as user_info["f_name"]
), and you also use them to build up the values for e_address
and/or password
. While it would be possible to put those first five values directly into the dictionary, getting them out again for use in the other calculations requires code that is even more redundant than what you have now.
So, unless you want to change your e_address
or password
calculations to be independent of the other values, I'd stick with what you have.
Upvotes: 3
Reputation: 9863
Not sure if you're looking for refactoring the code or reducing the length.
If it's the latter, you can do multiple assignment and reduce lines.
So something like:
def get_user_info(self):
month, day, year = choice(range(1,13)) , choice(range(1,29)), choice(range(1966,1994))
f_name, l_name = self.assign_name('FirstNames') , self.assign_name('LastNames')
e_address, password = f_name+l_name+year.__str__()+day.__str__(), f_name+l_name+'0008383'
And you don't really need to put in 'dict()', {}
does that already!
Upvotes: 1