creating a byte string in python - python-2.7

I'm trying to create a byte string, but it seems to be just a regular character string. What am I doing wrong here?
byteStr = b'some string'
byteStr #'some string'
utfStr = 'some string'.encode('utf-8')
utfStr #'some string'
byteStr == utfStr #True

If you're trying to create a byte array in Python 2, it's called a bytearray. Python 2 does not have a byte string.. The b in front of the str is ignored in Python 2, meaning 'hello' == b'hello'
Try this:
>>> f = b'f'
>>> type(f)
<type 'str'>
Now, it's important to remember that u'f' == 'f':
>>> h = u'f'
>>> f == h
True
>>> type(h)
>>> <type 'unicode'>

Related

Converting the input of DecimalField of WTForm to a float?-Flask

The input I give to a DecimalField of WTFform in Flask application is being converted to a string. I need it be an int or float in order to perform any
mathematical operation?
Any help would be appreciated.
TIA
wtforms.DecimalField produces a decimal.Decimal instance.
>>> import wtforms
>>> from webob.multidict import MultiDict
>>> class F(wtforms.Form):
... foo = wtforms.DecimalField()
...
>>> f = F(formdata=MultiDict(foo=3.45))
>>> val = f.data['foo']
>>> val
Decimal('3.45000000000000017763568394002504646778106689453125')
>>> type(val)
<class 'decimal.Decimal'>
The object can be used in calculations:
>>> val * 4
Decimal('13.80000000000000071054273576')
>>> val + 2
Decimal('5.450000000000000177635683940')
or can be converted to a float or int using the respective builtin functions
>>> float(val)
3.45
>>> int(val)
3

Python - Assign None or value

Is there a short way to assign None or value in a variable, depending on the value?
x= value if value!= 999 else None
result = (on_false, on_true)[condition]
>>> value = 10
>>> x = (None,value)[value != 999]
>>> print x
10
>>> value = 999
>>> x = (None,value)[value != 999]
>>> print x
None
You are using the correct way to do it.
but if you insist on shorten way to figure it out you can use this method:
first way:
{0:value}.get(value==999)
using the trick python saving same hash for False and 0 (hash = 0).
second way:
{999:None}.get(value,value)
using get method and default value to bring this.
third way:
[None, value][value != 999]
when first part stand for value declaration and the second as boolean condition.

How can I print output with a defined number of characters in each line with python?

I used textwrap.fill (textwrap.fill(text, 6)) to limit each line in only 6 characters, but there is a problem with using this command because my purpose is go to new line exact at 6 character, I mean:
for example using textwrap.fill(I am a student, 8):
what I want:
(I am a s
tudent)
output:
(I am a
student)
One approach:
>>> text = 'I am a student, 8'
>>> text = 'I am a student'
>>> for i in range(0, len(text), 8):
... print text[i:i+8]
...
I am a s
tudent
for i in range(0, len(text), 8) means "Give me numbers starting at 0, incrementing by 8 at a time, and ending before the length of the text."
EDIT
If you want the value in a single string:
>>> wrapped = "\n".join(text[i:i+8] for i in range(0, len(text), 8))
>>> wrapped
'I am a s\ntudent'

How to print out tags in python

If I have a string such as this:
text = "They refuse to permit us."
txt = nltk.word_tokenize(text)
With this if I print POS tags; nltk.pos_tag(txt) I get
[('They','PRP'), ('refuse', 'VBP'), ('to', 'TO'), ('permit', 'VB'), ('us', 'PRP')]
How can I print out only this:
['PRP', 'VBP', 'TO', 'VB', 'PRP']
You got a list of tuples, you should iterate through it to get only the second element of each tuple.
>>> tagged = nltk.pos_tag(txt)
>>> tags = [ e[1] for e in tagged]
>>> tags
['PRP', 'VBP', 'TO', 'VB', 'PRP']
Take a look at Unpacking a list / tuple of pairs into two lists / tuples
>>> from nltk import pos_tag, word_tokenize
>>> text = "They refuse to permit us."
>>> tagged_text = pos_tag(word_tokenize(text))
>>> tokens, pos = zip(*tagged_text)
>>> pos
('PRP', 'VBP', 'TO', 'VB', 'PRP', '.')
Possibly at some point you will find the POS tagger is slow and you will need to do this (see Slow performance of POS tagging. Can I do some kind of pre-warming?):
>>> from nltk import pos_tag, word_tokenize
>>> from nltk.tag import PerceptronTagger
>>> tagger = PerceptronTagger()
>>> text = "They refuse to permit us."
>>> tagged_text = tagger.tag(word_tokenize(text))
>>> tokens, pos = zip(*tagged_text)
>>> pos
('PRP', 'VBP', 'TO', 'VB', 'PRP', '.')
You can iterate like -
print [x[1] for x in nltk.pos_tag(txt)]

Converting a Set to Dict in python

I have a set like this.
x = set([u'[{"Mychannel":"sample text"},"p"]'])
I need to convert it into Dict.
I need to get output as
x = {'mychannel':'sampletext'}
How to do this.
It looks like you can unpack that crazy thing like this:
>>> x = set([u'[{"Mychannel":"sample text"}, "p"]'])
>>> lst = list(x)
>>> lst
[u'[{"Mychannel":"sample text"}, "p"]']
>>> lst[0]
u'[{"Mychannel":"sample text"}, "p"]'
>>> inner_lst = eval(lst[0])
>>> inner_lst
[{'Mychannel': 'sample text'}, 'p']
>>> d = inner_lst[0]
>>> d
{'Mychannel': 'sample text'}
However, as #MattDMo suggests in comments, I seriously suggest you re-evaluate this data structure, if not at least to factor out the step where you need eval to use it!