taking the argument (input) after a character in sys.argv[]? [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have a simple code to get user email information, I got a problem about something :
import sys
try:
argx = sys.argv(1)
except:
print("Please Input an email.")
Example For User argument :
py main.py example#example.com
I want to take arg (input) after the # char and the . char.
I need this to check the email provider, domain name and other stuff about the email.
Example of the thing i want :
import sys
try:
argx = sys.argv(1)
x = "The argument after the char #, and the ."
except:
print("Please Input an email.")
if(x.lower() == "gmail") :
gmail()
elif(x.lower == "protonmail") :
protonmail()

You can use split function of a string in order to split a string
like
x = 'name#example.com' #Your input
email = x.split('#') # will give ['name','example.com']
provider = email[1].split('.')[0] # will give 'example'

Asuming you have the following email example#example.com. In python you can split a string with the split function. The function itself needs a delimeter. In your case the delimeter would be #. The return value of the split function is an array.
parts_of_mail = email.split("#")
>>> [example, example.com]
You now have the array parts_of_mail, which stores the left and right part of the email. You can now split the string again, like above:
provider_info = parts_of_mail[1].split(".")
>>> [example, com]
Finally you can check the provider information:
if provider_info[0].lower() == "example":
# do stuff here
elif provider_info[0].lower() == "gmail":
# do stuff here
Note: provider_info[0] stores the provider and provider_info[1] stores the domain.

Related

'PySide6.QtWidgets.QLineEdit' object is not iterable [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Recently, I started programming in python, right now I'm writing a GUI for a password and pseudo-random number generator. I had this problem:
TypeError: 'PySide6.QtWidgets.QLineEdit' object is not iterable
btn_2 = QPushButton("Generate password", self)
btn_2.resize(100,30)
btn_2.move(340, 250)
btn_2.resize(100, 30)
btn_2.clicked.connect(self.btn1)
btn_3 = QPushButton("Generate pseudorandom numbers", self)
btn_3.move(140, 250)
btn_3.resize(180, 30)
btn_3.clicked.connect(self.btn2)
self.line = QLineEdit(self)
self.line.setPlaceholderText("Enter the expected string length")
self.line.resize(250, 20)
self.line.move(200, 220)
self.onlyInt = QIntValidator()
self.line.setValidator(self.onlyInt)
self.show()
def btn1(self):
mark = self.line
chars = string.ascii_letters + string.digits
print("".join(random.choice(chars) for _ in mark))
def btn2(self):
mark = self.line
chars = string.digits
print("".join(random.choice(chars) for _ in mark))
use self.line.text rather than self.line.
So:
mark = int(self.line.text)
chars = string.ascii_letters + string.digits
print("".join(random.choice(chars) for _ in range(mark))
Note that I've also replaced mark with range(mark) which is probably what you want here.
In general when given an XYZ isn't iterable error, go and look at the docs for XYZ and see how to get at whatever it is storing and you want to iterate over. In this case we see that we can get at the text which is presumably what you want with .text. But as it happens I think you want to iterate self.line.text times (hence your checking for an int), so we cast to int and then iterate through range(int(self.line.text)).

Can't print an object outside a function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm making a simple program in order to compare the currentdate to another date, and I need to separate the "/" and remove trailing zeros; I made a function for that:
def correctdate(compare, Dayslist, Monthslist):
for day in compare:
if day == "/" or day == "\\":
compare.remove(day)
break
else:
Dayslist.append(day)
for removenum in Dayslist:
#* Removing the numbers from the list
compare.remove(removenum)
for month in compare:
Monthslist.append(month)
#* Joining the numbers into a string
correctdate.DaysLeft = "".join(Dayslist)
correctdate.MonthsLeft = "".join(Monthslist)
#* Stripping leading zeros
correctdate.DaysLeft = correctdate.DaysLeft.lstrip("0")
correctdate.MonthsLeft = correctdate.MonthsLeft.lstrip("0")
return
The code works just fine but i want to save the DaysLeft, Monthsleft to print it/edit it ETC..
so i do this:
correctdate(compare,Dayslist,Monthslist)
print(correctdate.Daysleft)
and i get this:
AttributeError: 'function' object has no attribute 'Daysleft'
There was a typo in printing the object attribute
I wrote:
print(correctdate.Daysleft)
Its supposed to be:
print(correctdate.DaysLeft)
Sorry for the inconvenience
You ve to return it in your function, and outside it get it into variables:
def correctdate(compare, Dayslist, Monthslist):
for day in compare:
if day == "/" or day == "\\":
compare.remove(day)
break
else:
Dayslist.append(day)
for removenum in Dayslist:
#* Removing the numbers from the list
compare.remove(removenum)
for month in compare:
Monthslist.append(month)
#* Joining the numbers into a string
correctdate.DaysLeft = "".join(Dayslist)
correctdate.MonthsLeft = "".join(Monthslist)
#* Stripping leading zeros
correctdate.DaysLeft = correctdate.DaysLeft.lstrip("0")
correctdate.MonthsLeft = correctdate.MonthsLeft.lstrip("0")
return correctdate.DaysLeft,correctdate.MonthsLeft
This for return outside function, now you ve to call function correctly:
DaysLeft,Monthsleft = correctdate(compare,Dayslist,Monthslist)
print(DaysLeft,Monthsleft)
Anyway this code "correctdate.MonthsLeft" looks like you want use class and not only functions, so you should use like that https://www.w3schools.com/python/python_classes.asp
def correctdate(compare, Dayslist, Monthslist):
You declare a function with name correctdate that accepts 3 parameters.
correctdate.DaysLeft = "".join(Dayslist)
correctdate.MonthsLeft = "".join(Monthslist)
Then you try to assign a value to a function, which is not possible because correctdate is not a variable, not an object. You just declared it as a function.
What are you trying to achieve?

Python syntax error with function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
My python code wont work for some reason. It says the error is coming from the syntax of the function but im not sure why its doing that
one=1
two=2
three=3
four=4
five=5
six=6
seven=7
eight=8
nine=9
ten=10
print "test"
def convert()
number = raw_input('Enter the number you need converted to binary')
enterYourCommand = raw_input("Enter your command")
if enterYourCommand is "convert"
convert()
elif enterYourCommand is "tonumber"
tonumber()
You don't have : after function definition and if's:
one=1
two=2
three=3
four=4
five=5
six=6
seven=7
eight=8
nine=9
ten=10
print "test"
def convert():
number = raw_input('Enter the number you need converted to binary')
enterYourCommand = raw_input("Enter your command")
if enterYourCommand is "convert":
convert()
elif enterYourCommand is "tonumber":
tonumber()
All python functions should have a colon : at the end of their declaration line.
For example:
def convert():
number = raw_input('Enter the number you need converted to binary')
Also, the same is with your if and elif declarations:
if enterYourCommand is "convert":
convert()
elif enterYourCommand is "tonumber":
tonumber()
So, just add : at the end of each declaration and you should be good to go.
You missed the colons after if, elif and def. You have to indent with four spaces. See this link for examples.
one = 1
two = 2
three = 3
four = 4
five = 5
six = 6
seven = 7
eight = 8
nine = 9
ten = 10
enterYourCommand = raw_input("Enter your command")
def convert():
number = raw_input('Enter the number you need converted to binary')
if enterYourCommand == "convert":
convert()
elif enterYourCommand == "tonumber":
tonumber()
Hope it helps.
EDIT
Replace is with ==.
is will return True if two variables point to the same object
== will return True if the objects referred to by the variables are equal.
Sources : is-there-a-difference-between-and-is-in-python

Python 3 returning dictionary from a function case [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have python function that should return diction:
def load_cred(FILE):
key_for_enc = getpass(prompt='Key for encrypted credentials file: ', stream=None)
cipher = AESCipher(key_for_enc)
crd_dict={}
with open(FILE, 'r') as fh:
for line in fh:
dec_line = cipher.decrypt(line)
# print("line: {}".format(dec_line))
dec_line.strip()
start_string, user, password = dec_line.split(10*'|')
crd_dict[start_string] = (user, password)
#print("1: {} 2: {} 3: {}".format(start_string,user,password))
print("crd diction: {}".format(crd_dict))
return crd_dict
but when I call it from other script like that:
Data_cred = load_cred(CRED_FILE)
print ("Data type: {}".format(type(Data_cred)))
print("Data: ".format(Data_cred))
The returned dictionary don't appear as a returned value... Could anybody help me with this? Notice that within the function load_cred , crd_dict have it's items.. but outside it doesn't. I still don't get it why..
Key for encrypted credentials file:
crd diction: {'first_line': ('User1', 'Pass1')}
Data type: <class 'dict'>
Data len:
Data:
The function load_cred() is returning the dictionary. You just forgot to add the replacement field in the last line when printing it. -
print("Data: {}".format(Data_cred))

name.replace xX with y if x exists [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
To be more specific I'd like to figure out how to:
name.replace xX with y if x exists, if not then just replace X
I've searched this forum for an hour now, make it two, and all I find is how to replace one thing with another, which by now is pretty easy.
/a
you can just run:
output = name.replace('xX','y').replace('X','y')
Example:
name = "123xX345X"
output = "123y345y"
Sounds like a job for regular expression x?X:
>>> import re
>>> text = " test xX blabla"
>>> re.sub('x?X', 'y', text)
' test y blabla'
>>> text = " test X blabla"
>>> re.sub('x?X', 'y', text)
' test y blabla'
Quote from docs about ? mark:
The question mark character, ?, matches either once or zero times; you
can think of it as marking something as being optional. For example,
home-?brew matches either homebrew or home-brew.
if 'x' in name:
name = name.replace('xX','y')
else:
name = name.replace('X','y')
From your example above this is a slightly more involved problem. You have to make sure to do your renames in the root namespace or things can get nasty. You also run the risk of renaming parents before children, which will make it hard to get at the children with one call to ls. So:
def replace_with_any_namespace(src, tgt):
cmds.namespace(set=":")
results = {}
xforms = cmds.ls(r=True, tr=True, l=True) # use long paths and recursive to get all namespaces
xforms = [i for i in xforms if src in i] # only work on items with your target pattern
xforms.sort()
xforms.reverse() # sort and reverse means children get renamed before parents
for item in xforms:
path, sep, shortname = item.rpartition("|") # gets only the last name
newname = shortname.replace(src, tgt) # this should be fine even if the namespace is there
results[item] = cmds.ls(cmds.rename ( item, newname), l=True)[0]
# the paths and returns are all long paths so there are no ambiguities
return results
Are you trying to move things out of their namespaces with this? Thats easier:
cmds.namespace(mv = ("R", ":"), force=True)
which moves everything in R:* to the base namespace. This will probably result in some renames, however. You might want to put important nodes into a set before you call this so you can find them.

Categories