missing variable to complete cell execution - python

I hope someone can help me. I've been stuck with this error for a while. I have two .py files that I am importing in a jupyter notebook. When running the last cell (see code below) I get a Traceback error I can'f fix.
I think there is some error in my ch_data_prep.py file related to variable df_ch not correctly passed between files. Is this possible? Any suggestion on how to solve this problem?
Thanks!
ch_data_prep.py
def seg_data(self):
seg_startdate = input('Enter start date (yyyy-mm-dd): ')
seg_finishdate = input('Enter end date (yyyy-mm-dd): ')
df_ch_seg = df_ch[(df_ch['event_datetime'] > seg_startdate)
& (df_ch['event_datetime'] < seg_finishdate)
]
return df_ch_seg
df_ch_seg = seg_data(df_ch)
ch_db.py
def get_data():
# Some omitted code here to connect to database and get data...
df_ch = pd.DataFrame(result)
return df_ch
df_ch = get_data()
Jupyter Notebook data_analysis.ipynb
In[1]: import ch_db
df_ch = ch_db.get_data()
In[2]: import ch_data_prep
When I run cell 2, I get this error
--------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-3-fbe2d4fceba6> in <module>
----> 1 import ch_data_prep
~/clickstream/ch_data_prep.py in <module>
34 return df_ch_seg
35
---> 36 df_ch_seg = seg_data()
TypeError: seg_data() missing 1 required positional argument: 'df_ch'

Related

Can't create file into specific folder using gspread google colab

Yesterday I was still able to create a google sheet files using gspread into a specific folder using this code:
ss = gc.create(fileName,"my folder destination")
But today, this code yields an error.
Here is my full code:
from google.colab import auth
auth.authenticate_user()
import pandas as pd
import gspread
from google.auth import default
creds, _ = default()
gc = gspread.authorize(creds)
wb = gc.open('file name')
ws = wb.worksheet('sheet name')
# get_all_values gives a list of rows.
rows = ws.get_all_values()
df = pd.DataFrame.from_records(rows[1:],columns=rows[0])
modisseries = df["column name"]
uniquemodis = modisseries.drop_duplicates().tolist()
def createSpreadsheet(columName):
ndf = df[df["Modis"] == Columname]
nlist = [ndf.columns.tolist()] + ndf.to_numpy().tolist()
ss = gc.create(columnName,"Folder_id")
nws = ss.sheet1
nws.update_title(columnName)
nws.update("A1",nlist,value_input_option="USER_ENTERED")
for modis in uniquemodis:
createSpreadsheet(modis)
And here is the error message:
TypeError Traceback (most recent call last)
<ipython-input-1-5c297289782b> in <module>()
30
31 for column_value in uniquecolumn:
---> 32 createSpreadsheet(column_value)
<ipython-input-1-5c297289782b> in createSpreadsheet(column_value)
24 nlist = [ndf.columns.tolist()] + ndf.to_numpy().tolist()
25
---> 26 ss = gc.create(column_value,"folder_id")
27 nws = ss.sheet1
28 nws.update_title(column_value)
TypeError: create() takes 2 positional arguments but 3 were given
Hi which version of gspread do you use ?
The parameter folder_id has been introduced in version 3.5.0
From the error message you get it seems you have an old version and it does not handle the new parameter folder_id.
You can check the version you are currently using by using: print(gspread.__version__)
If this is bellow version 3.5.0 then run the following command:
python3 -m pip install -U gspread

CiscoConfigParser & Functions

I am trying to make the code cleaner and better manageable and i wanted to start with reading a cisco file. However when i try to put it in a function, it is not able to give me the outputs. The same works perfectly out of a function
Working model
parse = CiscoConfParse("C:\\python\\mydata\\TestConfigFile.txt")
TCPSrv = parse.find_objects("service\stcp\sdestination\seq")
UDPSrv = parse.find_objects("service\sudp\sdestination\seq")
ObjectNetwork = parse.find_objects("^object\snetwork\s")
ObjectGroupSrv = parse.find_objects("^object-group\sservice")
ObjectGroupNetwork = parse.find_objects("^object-group\snetwork\s")
This creates a list for all the above like the one below
TCPSrv = [<IOSCfgLine # 83 ' service tcp destination eq https' (parent is # 82)>,<IOSCfgLine # 97 ' service tcp destination eq www '(parent is # 102)>]
But when i put this into a function, it does not work. This is the first time i am trying out to use functions, and I know that i am doing something wrong.
This is my code for Functions
def cisco(filename):
parse = CiscoConfParse(filename)
TCPSrv = parse.find_objects("service\stcp\sdestination\seq")
UDPSrv = parse.find_objects("service\sudp\sdestination\seq")
ObjectNetwork = parse.find_objects("^object\snetwork\s")
ObjectGroupSrv = parse.find_objects("^object-group\sservice")
ObjectGroupNetwork = parse.find_objects("^object-group\snetwork\s")
return TCPSrv, UDPSrv, ObjectNetwork, ObjectGroupSrv, ObjectGroupNetwork
file = C:\\python\\mydata\\TestConfigFile.txt
cisco(file)
This does not give any output.
>>> TCPSrc
Traceback (most recent call last):
File "<input>", line 1, in <module>
NameError: name 'TCPSrc' is not defined
I have tried putting it like this below also
cisco("C:\\python\\mydata\\TestConfigFile.txt")
Can someone kindly assist what I am doing wrong.
This does not give any output
>>> TCPSrc
Traceback (most recent call last):
File "", line 1, in
NameError: name 'TCPSrc' is not defined
You have not assigned the return values to anything. When you call cisco(), you need to assign the return values to something... please use:
from ciscoconfparse import CiscoConfParse
def cisco(filename):
parse = CiscoConfParse(filename)
TCPSrv = parse.find_objects("service\stcp\sdestination\seq")
UDPSrv = parse.find_objects("service\sudp\sdestination\seq")
ObjectNetwork = parse.find_objects("^object\snetwork\s")
ObjectGroupSrv = parse.find_objects("^object-group\sservice")
ObjectGroupNetwork = parse.find_objects("^object-group\snetwork\s")
return TCPSrv, UDPSrv, ObjectNetwork, ObjectGroupSrv, ObjectGroupNetwork
values = cisco("C:\\python\\mydata\\TestConfigFile.txt")
TCPsrv = values[0]
UDPsrv = values[1]
# ... etc unpack the remaining values as illustrated above

Pulling stock option data with python pandas - broke overnight

Last night I typed up the following
from pandas.io.data import Options
import csv
symList = []
optData = {}
with open('C:/optionstrade/symbols.txt') as symfile:
symreader = csv.reader(symfile, delimiter=',')
for row in symreader:
symList = row
for symbol in symList:
temp = Options(symbol,'yahoo')
try:
optData[symbol] = temp.get_all_data()
except:
pass
It worked alright. I only got data from 200 something of the 400 something symbols I have in the file, but it pulled the options data for those 200 something just fine.
This morning, I go to run the code again (markets have been open for nearly an hour) and I get nothing:
In [6]: len(optData)
Out[6]: 0
So I run a bit of a test:
test = Options('AIG','yahoo')
spam = test.get_all_data()
import pickle
with open('C:/optionstrade/test.txt','w') as testfile:
pickle.dump(test,testfile)
I get this error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-4-902aa7c31f7e> in <module>()
1 test = Options('AIG','yahoo')
----> 2 spam = test.get_all_data()
C:\Anaconda\lib\site-packages\pandas\io\data.pyc in get_all_data(self, call, put)
1109
1110 for month in months:
-> 1111 m2 = month.month
1112 y2 = month.year
1113
AttributeError: 'str' object has no attribute 'month'
And this content of the pickled file:
ccopy_reg
_reconstructor
p0
(cpandas.io.data
Options
p1
c__builtin__
object
p2
Ntp3
Rp4
(dp5
S'symbol'
p6
S'AIG'
p7
sb.
Nothing has changed overnight on my end... last thing I did was save and shut down. First thing I did after waking up was run it again.

Weird TypeError in jython/python?

I'm bukkit jython/python plugin programmer. Last few days, I'm struggling with this problem. I have to add an potion effect to an user, it's not problem if I enter effect name, duration and amplifier manually in code, but when I want to get them from config, I get this error:
13:38:20 [SEVERE] Could not pass event PlayerInteractEvent to ItemEffect v1.0
Traceback (most recent call last):
File "<iostream>", line 126, in onPlayerInteractEvent
TypeError: addPotionEffect(): 1st arg can't be coerced to org.bukkit.potion.Poti
onEffect
Here's that part of code:
effectname = section.getString("%s.effect"%currentKey)
duration = section.getInt("%s.duration"%currentKey)
durationinticks = duration * 20
geteffectname = "PotionEffectType.%s"%effectname
getpotioneffect = "PotionEffect(%s, %i, 1)"%(geteffectname, durationinticks)
geteffectname = "PotionEffectType.%s"%effectname
if iteminhand == currentKey:
event.getPlayer().addPotionEffect(getpotioneffect)
When I print getpotioneffect out, I get:
13:38:20 [INFO] PotionEffect(PotionEffectType.SPEED, 600, 1)
which is okay, and should work. I tested it without getting informations from config, and it works perfectly... To sum up, code above is not working, but below one works:
getpotioneffect = PotionEffect(PotionEffectType.SPEED, 600, 1)
if iteminhand == currentKey:
event.getPlayer().addPotionEffect(getpotioneffect)
Link to javadocs of this event!
http://jd.bukkit.org/rb/apidocs/org/bukkit/entity/LivingEntity.html#addPotionEffect(org.bukkit.potion.PotionEffect)
Thanks!
In your first snippet, getpotioneffect is a string. You can check it adding print type(getpotioneffect) somewhere.
What you want is to replace this :
geteffectname = "PotionEffectType.%s"%effectname
getpotioneffect = "PotionEffect(%s, %i, 1)"%(geteffectname, durationinticks)
with this:
effect_type = getattr(PotionEffectType, effectname)
potion_effect = PotionEffect(effect_type, durationinticks, 1)

Python + GStreamer - Won't connect

I'm having trouble combining audio and video into one file. The Python code looks like this;
filmPipe = gst.Pipeline("filmPipe")
filmSrc = gst.element_factory_make("multifilesrc", "filmSrc")
filmSrc.set_property("location", "pictures/%d.png")
filmFilt1 = gst.element_factory_make("capsfilter", "filmFilt1")
filmCap1 = gst.Caps("image/png,framerate=5/1,pixel-aspect-ratio=1/1")
filmFilt1.set_property("caps", filmCap1)
filmPngDec = gst.element_factory_make("pngdec", "filmPngDec")
filmff = gst.element_factory_make("ffmpegcolorspace", "filmff")
filmFilt2 = gst.element_factory_make("capsfilter", "filmFilt2")
filmCap2 = gst.Caps("video/x-raw-yuv")
filmFilt2.set_property("caps", filmCap2)
filmTheora = gst.element_factory_make("xvidenc", "filmTheora")
filmQue = gst.element_factory_make("queue", "filmQue")
filmOggmux = gst.element_factory_make("ffmux_mp4", "filmOggmux")
filmFilesink = gst.element_factory_make("filesink", "filmFilesink")
filmFilesink.set_property("location", self.movPath)
musicSrc = gst.element_factory_make("filesrc", "musicSrc")
musicSrc.set_property("location", self.musicPath)
musicDec = gst.element_factory_make("ffdec_mp3", "musicDec")
musicEnc = gst.element_factory_make("lame", "musicEnc")
musicQue = gst.element_factory_make("queue", "musicQue")
filmPipe.add(filmSrc, filmFilt1, filmPngDec, filmff, filmFilt2, filmTheora, filmQue, filmOggmux, filmFilesink)
filmPipe.add(musicSrc, musicDec, musicEnc, musicQue)
gst.element_link_many(filmSrc, filmFilt1, filmPngDec, filmff, filmFilt2, filmTheora, filmQue, filmOggmux, filmFilesink)
gst.element_link_many(musicSrc, musicDec, musicEnc, musicQue, filmOggmux, filmFilesink)
filmPipe.set_state(gst.STATE_PLAYING)
This returns the following error:
Traceback (most recent call last):
File "app.py", line 100, in movGen
gst.element_link_many(musicSrc, musicDec, musicEnc, musicQue, filmOggmux, filmFilesink)
gst.LinkError: failed to link filmOggmux with filmFilesink
Does anybody know where I'm going wrong, or how to fix this?
You are linking 2 times filmOggmux to filmFilesink: this is not allowed, only one link is possible.
Try removing filmFilesink in the second gst.element_link_many().

Categories