Recieivng a 'NoneType' 'append' in python 3 - append

I have this code and it keeps giving me the 'NoneType' object has no attribute 'append'. Can someone tells me what is wrong, please?
def extract():
extracted_data = pd.DataFrame(columns = ['Name','Last'])
for jsonfile in glob.glob('file_1.json'):
extracted_data = extracted_data.append(extract_from_json(jsonfile), ignore_index = True)
return extract

Related

pointer arrays to String

I need to have a pointer array like in C, in Swift.
The following code works:
let ptr = UnsafeMutableBufferPointer<Int32>.allocate(capacity: 5)
ptr[0] = 1
ptr[1] = 5
print(ptr[0], ptr[1]) // outputs 1 5
The following code, however, does not work:
let ptr = UnsafeMutableBufferPointer<String>.allocate(capacity: 5)
print(ptr[0]) // Outputs an empty string (as expected)
print(ptr[1]) // Just exits with exit code 11
When I do print(ptr[1]) in the swift REPL, I get the following output:
Execution interrupted. Enter code to recover and continue.
Enter LLDB commands to investigate (type :help for assistance.)
How can I create a C-like array with Strings (or any other reference type, as this also doesn't seem to work with classes).
What should I adjust?
You need to initialize the memory with valid String data.
let values = ["First", "Last"]
let umbp = UnsafeMutableBufferPointer<String>.allocate(capacity: values.count)
_ = umbp.initialize(from: values)
print(umbp.map { $0 })
umbp[0] = "Joe"
umbp[1] = "Smith"
print(umbp.map { $0 })
Prints:
["First", "Last"]
["Joe", "Smith"]

Tracking down "missing parameter type for expanded function"

Im stuck with with code, whenever I try to run it I get the error:
Error:(37, 66) missing parameter type for expanded function
The argument types of an anonymous function must be fully known. (SLS 8.5)
Expected type was: com.bfg.domain.model.RunnerSnap
def syncPrices(pair: (RunnerSnap, RunnerChange)): RunnerSnap = {
However the error message is not enogh for me to know what I need to change to get this working.
def syncPrices(pair: (RunnerSnap, RunnerChange)): RunnerSnap = {
case (cache: RunnerSnap, delta: RunnerChange) =>
val newHc = if (delta.hc.isEmpty) cache.runnerId.handicap else delta.hc
val id = RunnerId(delta.id, newHc)
val prices = MarketRunnerPrices(
atl = syncPriceVolume(cache.prices.atl, delta.atl),
atb = syncPriceVolume(cache.prices.atb, delta.atb),
trd = syncPriceVolume(cache.prices.trd, delta.trd),
spb = syncPriceVolume(cache.prices.spb, delta.spb),
spl = syncPriceVolume(cache.prices.spl, delta.spl),
batb = syncLevelPriceVolume(cache.prices.batb, delta.batb),
batl = syncLevelPriceVolume(cache.prices.batl, delta.batl),
bdatb = syncLevelPriceVolume(cache.prices.bdatb, delta.bdatb),
bdatl = syncLevelPriceVolume(cache.prices.bdatl, delta.bdatl),
spn = if (delta.spn.isEmpty) cache.prices.spn else delta.spn,
spf = if (delta.hc.isEmpty) cache.prices.spf else delta.spf,
ltp = if (delta.hc.isEmpty) cache.prices.ltp else delta.ltp,
tv = if (delta.hc.isEmpty) cache.prices.tv else delta.tv
)
RunnerSnap(id, prices)
}
My question is: Why am I getting this error and what do I need to
change in my code to get it working as expected?
You are missing pair match:
def syncPrices(pair: (RunnerSnap, RunnerChange)): RunnerSnap = pair match {
case (cache: RunnerSnap, delta: RunnerChange) =>
...
}

Cant convert AnyObject(Optional) to String in Swift2

I am stuck on this code! I want to convert an AnyObject to String what is the best way to do that?
In this little Code-snippet below I try to convert the Result of an Realm query to String. But at the end I can't remove the Optional. Have a look:
print(Object)
// Print:
//Results<Subject> (
// [0] Subject {
// id = 10;
// name = Englisch;
// short = Eng;
// mainSubject = 1;
//}
let name = Object.valueForKey("name")
print(name)
// Print:
//Optional((
// Englisch
//))
let newname = name as! String
// Here I try to convert the AnyObject from above to a Swift-String but this don't work!
// Error: Could not cast value of type '__NSArrayM' (0x7fff7db48c18) to 'NSString' (0x7fff7e4ed268).
print(newname)
// Will never be executed
Can someone help me please?
Thanks
Michael
Here's the answer:
let name = Object.valueForKey("name") as! [String]
let newName = name.first!
print(newName)
This is not elegant but it works ;-)
and sorry that I can't vote yet. But when I've earned 15 reputation, my votes will become public
You can try
let name = Object.valueForKey("name").stringValue!
print(name) //Should print: Englisch
Don't know whether this will work for you or not. But it should, I guess.
Your problem is that the name property is of type NSMutableArray (__NSArrayM in the error message) so you should cast it to [String].

Calling a method in a class, in an if/elif statement

i'm creating trying to create a project, but i'm running into an error.
This is my code (not all of it, it's pretty lengthy, but the problem i'm running into):
class Rest(object):
def __init__(self, name, order=[], total=0):
self.name = name
self.order = []
self.total = 0
def end_order(self):
print("Here is your complete order: {0}".format(self.order))
print("Here is your total: {0}".format(self.total))
def order_menu(self):
loop = 1
while (loop == 1):
question_1 = raw_input("What would you like? Push S to Submit, Push C to Cancel")
if (question_1 == "1"):
self.total += 4.99
print("You added a cheeseburger, $4.99)
elif (question_1 == "S"):
end_order()
Okay, so under order_menu(self), under the elif statement, it gives me an error:
"Global name 'end_order()' is not defined".
There's a probably something silly i'm not doing, but I can't figure out what..
I believe the self keyword is required in Python when calling a class method. Try:
self.end_order()

Accessing JSON data using Swift

I am trying to access a key from the following JSON:
{ items = ({a = “string for a”;b = “string for a”;c = “string for a”;},{a = “string for a”;b = “string for a”;c = “string for a”;},{a = “string for a”;b = “string for a”;c = “string for a”;});}
For example, I would like to get the value for the first key 'a', but the following returns nil:
println(jsonResult["products[product_id]"]
Your JSON Example is not valid. Check out with this:
http://jsoneditoronline.org/
I would use a common JSON Plugin for Swift. For Example this:
https://github.com/owensd/json-swift
And then use:
let json : JSON = "your string"
And get your objects with:
if let myproduct = json["products"]["productid"].array {
// content of your product (if array)
}