Loading NSString class on iOS fails, while NSSet and other works? - ctypes

Using ctypes, I am getting a handle for objc_getClass method, and then I am trying to locate NSString but it FAILS!! At the same time, other classes such as NSSet/NSMutableArray and so on just work great!
import ctypes
from ctypes import cdll
c = cdll.LoadLibrary(None)
objc_getClass = c.objc_getClass
>>> c.objc_getClass(b'NSSet')
-110110920
>>> c.objc_getClass(b'NSString')
0

That's probably because the Foundation framework is not loaded.
Try adding this before trying to resolve classes:
cdll.LoadLibrary('/System/Library/Frameworks/Foundation.framework/Foundation')

Related

Can't write to a csv file from a class in kotlin

Im trying to write a class to csv. And I need or at least prefer a class that I could call that could both retrieve and eventually save the class objects as csv.
I have tried every imaginable way I can find to write files. Everytime it works flawlessly in the main activity .kt, but soon as I move any of it to a dedicated class I get:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.FileOutputStream android.content.Context.openFileOutput(java.lang.String, int)' on a null object reference
or I get told thats is a read only file system.
Heres a copy of a class that ive tried and gotten the error.
package com.example.jobndays
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.PrintWriter
class DryTest: AppCompatActivity() {
fun main() {
var fileInputStream: FileOutputStream? = null
fileInputStream = openFileOutput("bobbin.csv", MODE_PRIVATE)
val bob = "fella"
fileInputStream.write(bob.toByteArray())
}
}
I believe I've found an answer. Apparently in order to write files you need 'context', not entirely sure why, but just ask for it and stick infront of stuff that says they're unresolved.

How to load a GluonTS DeepAR model

I am new to GluonTS and deep learning in general. I have a GluonTS DeepAR model which has files like -
myPrefix-xxxx.params
myPrefix-network.json
What is the way to load this model for scoring? I tried below but this looks for prefix-symbol.json
import mxnet as mx
model = mx.mod.Module.load(myPrefix,epochs)
Thanks.
The way to load a GluonTS model is to use the Predictor class and deserialize it -
from gluonts.model.predictor import Predictor
from pathlib import Path
model = Predictor.deserialize(Path(path_to_the_model))

How to modify the code so that i do not have to uninstall pycharm?

I have run the code as part of exception handling in python, during a session on Plural-sight but now even if a write a incorrect code the result is OK.
For example : hello world without print statement gives me exit code 0
Could you please advice on it?
code :
try:
import msvcrt
def getkey():
return msvcrt.getch()
except ImportError:
import sys
import tty
import termios
def getkey():
fd=sys.stdin.fileno()
original_attributes =termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, original_attributes)
return ch
You need to make sure that the indentation of your code is correct first.
For example get_key function should be defined in both: the try case and in the catch, in the catch case you indented it correctly under the catch statement, but for the try case you made it on the same level of indentation as the try itself, for the interpreter it means that the function definition is not related to the try you wrote, therefore it throws an error as you should not add code after between the try and catch statement.
a rewrite of your program
try:
import msvcrt
def getkey():
return msvcrt.getch()
except ImportError:
import sys
import tty
import termios
def getkey():
fd=sys.stdin.fileno()
original_attributes =termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, original_attributes)
return ch
This code says now, if we tried to import the library msvcrt and succeeded, then we will define a function get_key using the library, if failed; then we will define it again without using the library.

readValue() no working with TypeReference

I am writing following code to convert my JSON sting to list of my object.
List<MyObject> myResponse = new ArrayList<MyObject>();
myResponse = new ObjectMapper().readValue(responseString, new TypeReference<List<MyObject>>(){});
But eclipse is complaining with following error:
The method readValue(String, Class<T>) in the type ObjectMapper is not applicable for the arguments (String, new TypeReference<List<MyObject>>(){})
What can be possible error?
Issue was with import of TypeReference. I was using
import com.fasterxml.jackson.core.type.TypeReference
instead of
import org.codehaus.jackson.type.TypeReference
For me the issue was with import of ObjectMapper. I was using
import com.fasterxml.jackson.core.type.ObjectMapper
instead of
import org.codehaus.jackson.type.ObjectMapper

Serializing and unserializing case classes with lift-json

I'm attempting basic serialization/hydration with lift-json, but without success. As near as I can tell from the package readme, this should work. Help?
I'm using Scala 2.8.0 and Lift 2.2 cross-built for 2.8 with sbt ("net.liftweb" %% "lift-json" % "2.2").
import net.liftweb.json._
import net.liftweb.json.Serialization.{read, write}
implicit val formats = Serialization.formats(NoTypeHints)
case class Route(title: String)
val rt = new Route("x277a1")
val ser = write(rt)
// ser: String = {} ...
val deser = read[Route]("""{"title":"Some Title"}""")
// net.liftweb.json.MappingException: Parsed JSON values do not match with class constructor
Lift JSON's serialization does not work for case classes defined in REPL (paranamer can't find the bytecode to read the type metadata). Compile Route with scalac and then the above example works.
The same problem applies every time when the (de)serialuzed class is not on the classpath. In such case, paranamer can't read the parameter names. It is necessary to provide a custom ParameterNameReader.
Such problem applies for e.g.:
REPL (as mentioned) - unless you define the class outside the REPL and add via classpath.
Play Framework - unless you provide a simple custom ParameterNameReader (see below) or load the (de)serialized class as a Maven/Play/... dependency
Feel free to add another situation (you can edit this post).
The PlayParameterNameReader:
import net.liftweb.json.ParameterNameReader
import java.lang.reflect.Constructor
import play.classloading.enhancers.LocalvariablesNamesEnhancer
import scala.collection.JavaConversions._
object PlayParameterReader extends ParameterNameReader{
def lookupParameterNames(constructor: Constructor[_]) = LocalvariablesNamesEnhancer.lookupParameterNames(constructor)
}