Testing the data type of a value
When I run the following code, I get the following output:
>>> print(type(5))
class 'int'
Next, I try to compare the data-type of 5 with the earlier output, I get no
output:
>>> if type(5) == "":
print("Integer")
Why isn't this working? Advance thanks for your time.
and regards from
Binoy
--
https://mail.python.org/mailman/listinfo/python-list
Re: Testing the data type of a value
On 12-5-2019 09:27, [email protected] wrote: When I run the following code, I get the following output: print(type(5)) class 'int' Next, I try to compare the data-type of 5 with the earlier output, I get no output: if type(5) == "": print("Integer") Why isn't this working? Advance thanks for your time. and regards from Binoy print(isinstance(5,int)) True isinstance(object, classinfo) Return true if the object argument is an instance of the classinfo argument, or of a (direct, indirect or virtual) subclass thereof. If object is not an object of the given type, the function always returns false. If classinfo is a tuple of type objects (or recursively, other such tuples), return true if object is an instance of any of the types. If classinfo is not a type or tuple of types and such tuples, a TypeError exception is raised. -- Luuk -- https://mail.python.org/mailman/listinfo/python-list
Re: Testing the data type of a value
[email protected] writes: > When I run the following code, I get the following output: print(type(5)) > class 'int' > > Next, I try to compare the data-type of 5 with the earlier output, I > get no output: if type(5) == "": > print("Integer") > > Why isn't this working? Advance thanks for your time. See if this helps... >>> type(5) >>> type(type(5)) >>> type("") so you are comparing two things with different types. An analogous situation would be to >>> print(5) 5 and then to try to compare 5 with the earlier output by writing if 5 == "5": ... -- Ben. -- https://mail.python.org/mailman/listinfo/python-list
Re: Testing the data type of a value
On 12-5-2019 10:16, Luuk wrote: On 12-5-2019 09:27, [email protected] wrote: When I run the following code, I get the following output: print(type(5)) class 'int' Next, I try to compare the data-type of 5 with the earlier output, I get no output: if type(5) == "": print("Integer") Why isn't this working? Advance thanks for your time. and regards from Binoy print(isinstance(5,int)) True isinstance(object, classinfo) Return true if the object argument is an instance of the classinfo argument, or of a (direct, indirect or virtual) subclass thereof. If object is not an object of the given type, the function always returns false. If classinfo is a tuple of type objects (or recursively, other such tuples), return true if object is an instance of any of the types. If classinfo is not a type or tuple of types and such tuples, a TypeError exception is raised. After thinking about this, (i am prettry new to python), i was doing this: >>> print(type(5),type(int),type(5)==type(int),type(5)==int) False True Can someone explain why type(5)==int evaluates to True ? -- Luuk -- https://mail.python.org/mailman/listinfo/python-list
Re: Testing the data type of a value
Luuk writes: > > After thinking about this, (i am prettry new to python), i was doing this: > print(type(5),type(int),type(5)==type(int),type(5)==int) > False True > > Can someone explain why type(5)==int evaluates to True ? > >>> print(int) The value of int is the class int, which is the class of 5, so type(5) is also that same class int. -- Piet van Oostrum WWW: http://piet.vanoostrum.org/ PGP key: [8DAE142BE17999C4] -- https://mail.python.org/mailman/listinfo/python-list
Re: Testing the data type of a value
On 12-5-2019 16:07, Piet van Oostrum wrote: Luuk writes: After thinking about this, (i am prettry new to python), i was doing this: print(type(5),type(int),type(5)==type(int),type(5)==int) False True Can someone explain why type(5)==int evaluates to True ? print(int) The value of int is the class int, which is the class of 5, so type(5) is also that same class int. Maybe i should have asked this: What is the difference between 'type(5)==int' and 'isinstance(5,int)' and, if there is no difference why did someone invent 'isinstance()' ... -- Luuk -- https://mail.python.org/mailman/listinfo/python-list
Python 2 EOL; what about P2 packages
I had missed the End-of-Life announcement of Python 2 for 2020. When installing a missing package on my new laptop, pip did warn me about this upcoming event. I have been reading up on the conversion. Unfortunately, I can find no information yet on coping with Python 2 only packages. So far, I identified that I am using at least 2 packages that are Python 2 only: + HTMLgen (the old original version that has been an orphan for decades; simple but effective) + eGenix (excellent package with a.o. MxDateTime; I can only find a sprint attempt to convert t to Python 3) Any suggestions how to port to Python 3, when using Python 2 only packages? Although the websites state that we can keep using Python 2 after maintenance stops, this may in practice not be the case. Any time that we need to get a new laptop operational, we need a way to install it. E.g. pip should stay working... Gerrit -- https://mail.python.org/mailman/listinfo/python-list
Re: Testing the data type of a value
Luuk writes: > On 12-5-2019 16:07, Piet van Oostrum wrote: >> Luuk writes: >> >>> After thinking about this, (i am prettry new to python), i was doing this: >>> >> print(type(5),type(int),type(5)==type(int),type(5)==int) >>> False True >>> >>> Can someone explain why type(5)==int evaluates to True ? >>> > print(int) >> >> >> The value of int is the class int, which is the class of 5, so type(5) is >> also that same class int. > > > Maybe i should have asked this: > > What is the difference between 'type(5)==int' and 'isinstance(5,int)' > > and, if there is no difference why did someone invent 'isinstance()' ... > You'll get different behaviour when subclassing is involved. https://docs.python.org/3/library/functions.html#isinstance isinstance(object, classinfo) Return true if the object argument is an instance of the classinfo argument, or of a (direct, indirect or virtual) subclass thereof. -- regards, kushal -- https://mail.python.org/mailman/listinfo/python-list
Automate extract domain
I am trying to extract domain name from a adblock rule , so what
pattern should i used to extract domain name only?
import re
domains = ['ru', ' fr' ,'eu', 'com'] with open('easylist.txt', 'r') as f:
a=f.read() result=re.findall(r'[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+',a)
unique_result = list(set(result))
for r in unique_result: #Extract domain name out of url domain_name =
r.split('.')[1] #Check if domain name is in list of domains, only then add it
if domain_name in domains: print(r)
this one is labours process for that I have to find extension of all domain nd
then add it into the domains. So I want something which could automate extract
domain only
--
https://mail.python.org/mailman/listinfo/python-list
Re: Automate extract domain
On 2019-05-12, Birdep wrote:
> I am trying to extract domain name from a adblock rule , so what
> pattern should i used to extract domain name only?
>
> import re
> domains = ['ru', ' fr' ,'eu', 'com'] with open('easylist.txt', 'r') as f:
> a=f.read() result=re.findall(r'[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+',a)
> unique_result = list(set(result))
> for r in unique_result: #Extract domain name out of url domain_name =
> r.split('.')[1] #Check if domain name is in list of domains, only then add it
> if domain_name in domains: print(r)
>
> this one is labours process for that I have to find extension of all
> domain nd then add it into the domains. So I want something which
> could automate extract domain only
What do you mean by "domain name"? Do you mean just the top level?
In which case you can just do fullname.rsplit(".", 1)[-1]. If you
mean "the registrable domain" (such as example.com, example.co.uk,
etc) then you will need to look at https://publicsuffix.org/
--
https://mail.python.org/mailman/listinfo/python-list
Re: Python 2 EOL; what about P2 packages
On Mon, May 13, 2019 at 12:41 AM GerritM wrote: > > I had missed the End-of-Life announcement of Python 2 for 2020. When > installing a missing package on my new laptop, pip did warn me about this > upcoming event. > > I have been reading up on the conversion. Unfortunately, I can find no > information yet on coping with Python 2 only packages. So far, I identified > that I am using at least 2 packages that are Python 2 only: > + HTMLgen (the old original version that has been an orphan for decades; > simple but effective) > + eGenix (excellent package with a.o. MxDateTime; I can only find a sprint > attempt to convert t to Python 3) > > Any suggestions how to port to Python 3, when using Python 2 only packages? > > Although the websites state that we can keep using Python 2 after maintenance > stops, this may in practice not be the case. Any time that we need to get a > new laptop operational, we need a way to install it. E.g. pip should stay > working... > At some point, you may have to make a choice between running unmaintained software and migrating to something that is actually being updated. Python 2.7 won't magically stop working just because 2020 rolls around; it simply won't be receiving any updates. So you COULD just keep using it regardless. But to move to Py3, you're probably going to have to find approximate equivalents to those two packages, or else help those packages to migrate. I did a quick search for "htmlgen python 3" and found a Red Hat bug report, but nothing particularly helpful. My recommendation: Look at your two Py2-only packages, and see whether they're actively maintained. If they are, then their maintainers will probably want to look at porting them (and you may be able to help with that effort); if not, it's time to look for replacements. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Python 2 EOL; what about P2 packages
On 12.05.2019 16:38, GerritM wrote: Any suggestions how to port to Python 3, when using Python 2 only packages? You need to decide for each package whether to port it or to replace it. HTMLgen is probably a pure Python package. It should not be too hard to port it to Python 3 yourself. At the time, mx.DateTime filled a gap. Python has had a standard module 'datetime' for some years now. When moving to Python 3 I replaced my usages of mx.DateTime with datetime and pytz. Regards, Dietmar -- https://mail.python.org/mailman/listinfo/python-list
