python paho-mqtt publish NULL value - python-2.7

I'm trying to read from a CSV file and publish row values using paho-mqtt but broker log responds that it cannot update the topic with NULL
for r in reader:
publish.single(strMqttChannel, r[2], hostname=strMqttBroker, auth={'username': 'user', 'password': 'password'})
r[2] is in the simplest case i tried 1
it can also be a minus decimal like -1.5
str(r[2]) also doesn't work
If I pass the value without variable it works like charm:
publish.single(strMqttChannel, "1", hostname=strMqttBroker, auth={'username': 'user', 'password': 'password'})
What am I doing wrong?

r[2].strip()
did the trick....

Related

Django .save(using='...') correct database name

My Django settings.py contains the following:
DATABASES = {
'default': { # several settings omitted
'NAME': 'myproject', 'HOST': 'localhost', },
'other': { 'NAME': 'other', 'HOST': 'else.where', }
}
I now want to fetch some objects from the other DB and save it to default like so:
things = Thing.objects.using('other').raw('SELECT code,created FROM other.otherapp_bigthing WHERE created>...')
# 'code' is a unique string in other and PK of the Thing model
for t in things:
t.save(using='default')
This gives me
ProgrammingError: (1146, "Table 'other.myapp_thing' doesn't exist")
which is a correct observation, however, by the documentation of the using parameter I expected the records to be saved to myproject.myapp_thing. Why is the database name still taken from the other configuration when I explicitly advised it to use default?
I eventually worked around this by making a raw insert (connections['default'].cursor().execute('INSERT INTO myproject.myapp_thing (...) ON DUPLICATE KEY UPDATE...') but still wanted to know why the question's code does not work.
The problem was that the Thing model contained another field: next to the code CharField/PK and created date (in my use case actually last_accessed) there was another date (think modified) with default=None, null=True and I assumed the default would be used if I do not explicitly include in the original raw query.
Apparently, this assumption was wrong and Django tried, on t.save, to look up a value for modified in the DB it originally came from -- and failed, since it came from a raw SQL query.
If I include ,NULL AS 'modified' in the raw SQL query the original error goes away but I still cannot reasonably use Django logic for my use case since t.save(using='default') would, for existing things, update last_accessed and overwrite the modified date with NULL, which I do not want, and t.save(using='default', update_fields=['last_accessed']) would force an update and thus fail for new things.

how i can give a variable for this list

i have list like this
http://google.com:username:password
http://google2.com:username2:password2
how i can give a variable for this list i went give 3 variable Address , username , password
when i do print Address i went this program print for me google.com
and when i do print username i went this program print for me username
As mentioned in the comments, the most straightforward solution may be to put your list into a python dictionary structure with your 3 variables as keys, and each key is associated with individual values:
mydict = [
{
'Address': 'http://google.com',
'username': 'username',
'password': 'password'
},
{
'Address': 'http://google2.com:username2:password2',
'username': 'username2',
'password': 'password2'
}
]
Demo:
We have 2 entries from the original list, so you can specify which entry you want by indexing the dictionary starting from 0 as the first entry
# access 1st entry values
print mydict[0]['Address'] # http://google.com
print mydict[0]['username'] # username
# access 2nd entry values
print mydict[1]['password'] # password2

Reading a site into a dataframe, after logging in via Requests

I'm trying to create a data frame from a html page that has my weekly football picks.
I have bypassed the login via "requests" and the following code:
import pandas as pd
import requests
with requests.session() as c:
url ='https://auth.cbssports.com/login?master_product=150&xurl=http%3A%2F%2Fwww.cbssports.com%2F'
c.get(url)
login_data ={'dummy::login_form':'1', 'form::login_form': 'login_form', 'xurl':'http://funkybunk.football.cbssports.com/office-pool/standings/live', 'master_product':'XXXX', 'vendor': 'cbssports', 'userid':'XXXX#gmail.com', 'password': 'XXXX', '_submit': 'Sign+In'}
c.post(url, data = login_data, headers ={'Referer':'https://auth.cbssports.com/login?master_product=150&xurl=http%3A%2F%2Fwww.cbssports.com%2F'})
page =c.get("http://funkybunk.football.cbssports.com/office-pool/standings/live/1")
However, I don't know how to get the info from "page" into a dataframe.
When I use "pd.read_html" it still reads the login page instead of the page in the parameter of pd.read_html.
week1 = pd.read_html("http://funkybunk.football.cbssports.com/office- pool/standings/live/1")
print(week1)
# trying to read it into a data frame; however it prints out the login page not the page I accessed via "page"
If I try:
week1 =pd.read_html(page)
#get an error saying I can't get response objects
If I run
print(page.content)
# I'm able to see all of my info
#such as my team picks etc
""" I"m able to see all this info and more: id":XXX"41","snsScore":0,"numGames":16,"picks":{"NFL_MIA#SEA":{"winner":"SEA"},"time":"1473309152","NFL_BUF#BAL":{"winner":"BAL"},"NFL_OAK#NO":{"winner":"OAK"},"NFL_NYG#DAL":{"winner":"NYG"},"NFL_SD#KC":{"winner":"SD"},"NFL_GB#JAC":{"winner":"GB"},"NFL_MIN#TEN":{"winner":"MIN"},"NFL_CAR#DEN":{"winner":"CAR"},"mnf":"43","NFL_NE#ARI":{"winner":"ARI"},"NFL_DET#IND":{"winner":"IND"},"NFL_CHI#HOU":{"winner":"HOU"},"NFL_TB#ATL":{"winner":"TB"},"NFL_LAR#SF":{"winner":"LAR"},"NFL_CLE#PHI":{"winner":"PHI"},"NFL_PIT#WAS":{"winner":"PIT"},"NFL_CIN#NYJ":{"winner":"CIN"}}}"""
#However when I run try to read it via a dataframe
#week1 = pd.read_html(page.contents)
#I only see:
#>>> week1
#[ 0 1 2
#0 NaN NaN NaN
#1 NaN Live Scoring Notice NaN
#2 NaN NaN NaN
#3 NaN NaN NaN]
So all the data isn't being read into the dataframe for some reason.
I don't know anything about html coding and was able to use 'request' by watching a video and some trial and error.
I've been at it a couple of days but I'm stumped.
Thanks.
I'm using python 2.7 and Windows 7.
Thanks.

Mongoengine create_user function generating null objectId's

It always seems to get the exact same error after I've added a single user to the DB.
Tried to save duplicate unique keys (E11000 duplicate key error index: mydb.user.$objectId_1 dup key: { : null })
What's strange is that I copied the code EXACTLY from the test suite and even that doesn't work. I look at the stack trace and the offending issue is always in the same place
/usr/local/lib/python2.7/dist-packages/mongoengine/django/auth.py in set_password
264. self.save()
It's incredibly frustrating and I have been looking at this for basically the last 2 days. It's not something in my code. For some reason it doesn't seem to be generating an ObjectId as it's always null. I don't even understand why that's the case
My code is simply
from mongoengine.django.auth import User
from django.contrib.auth import get_user_model
user_data = {
'username': 'user',
'email': 'user#example.com',
'password': 'test',
}
manager = get_user_model()._default_manager
user = manager.create_user(user_data)
Looks like you have an extra index on an objectId field that is Unique
When creating a new User its not setting an objectId field value and as its unique you can't have two documents without a value (as its not a sparse index).
To test in the mongo shell:
> use mydb
> db.user.getIndexes()
Drop the objectId_1 index:
> db.user.dropIndex("objectId_1")
> db.user.getIndexes()

How to fix Django error: " 'unicode' object has no attribute 'tzinfo' " on database read

I am deploying my Django project on Windows Azure. I was using SQLite as a database server and everything was ok. When I have deployed My project, I decided to connect it with an SQL Azure Database but it seems that this solution created some errors. I am no longer able to edit users profiles. I get always this error :
AttributeError at /admin/auth/user/1/
'unicode' object has no attribute 'tzinfo'
This error happens when your database contains date-time stamps like this:
0000-00-00 00:00:00.000000
(this can happen in MySQL if you delete or overwrite a previous date with MySQLWorkbench)
When you try to retrieve these records in a Django model object, you will get an exception from the pytz timezone library:
AttributeError 'unicode' object has no attribute 'tzinfo'
You should edit these dates in your database first, and set them to more recent dates, like 2018-01-01 00:00:00.000000 or set to NULL (but not blank).
See:
https://groups.google.com/forum/#!topic/django-users/Jg_9fQ3jMcU
See also:
#1292 - Incorrect date value: '0000-00-00'
Error in mysql when setting default value for DATE or DATETIME
How to store NULL values in datetime fields in MySQL?
I had same issue trying to use django-pyodbc-azure database backend with Django (1.5.1): by default it stores DateTimeField fields in your DB as datetime2(7), which looks to be still unsupported in Django.
In my case I added the option 'use_legacy_datetime' : True in settings.py, like below:
DATABASES = {
'default': {
'ENGINE' : 'sql_server.pyodbc',
'NAME' : '<MYDBNAME>',
'USER': '<MYDBUSER>',
'PASSWORD': '<MYDBPWD>',
'HOST': '<MYHOST>',
'OPTIONS': {
'use_mars': True,
'use_legacy_datetime' : True, # I added this line
},
},
}
I found my solution here.
I don't know Azure platform well, so I don't know if this is exactly your case, if not you can still modify your database replacing datetime2(N) fields with good old datetime ones.
Hope it helps.