Python module naming - python-2.7

Using python 2.7, I'm building a package as follows:
mypackage/
__init__.py
somemodule.py
In __init__.py I have this:
from . import somemodule
In somemodule.py I have The following:
class SomeClass:
pass
When I try and access SomeClass, it prepends the filename to the class, so it's accessible by this:
import mypackage
mypackage.somemodule.SomeClass
How can I get it so that it's accessible as follows:
mypackage.SomeClass
Without putting the class directly in __init__.py?
I'm sure this has been answered before, but I'm not sure how to phrase the question.

If you really want to import your class like
mymodlue.myClass
you have to import the class in your __init__.py-file first, for instance like this:
from someclass import myClass
You could even do this (just to get your head around the concept - it's actually really ugly):
import someclass
myClass = someclass.myClass
Works just as well.

Related

How to create import to CodeQL

I want to create an import to my CodeQL query.
I want that this import will be named Utils and I will created inside it a predicate named isNumber.
How can I creat such import?
This how I want my code to look like:
import cpp
import Utils
where
if exists(...)
then isNumber(size.(VariableAccess).getTarget())
else ...
select ...
I don't know how I create the Utils import, it writes:
Could not resolve module Utils
I tried to create a folder named Utils near my code query (code.ql) but it didn't work.
I found how to do it.
Need to create a file named Utils.qll in the same folder of your CodeQL query.
This is its code:
import cpp
predicate isNumber(Variable v){
v.getUnspecifiedType() instanceof IntegralType
}

How to instantiate a class one time and access it in views

I have a class that runs once, which I had in myapp/__init__.py, but each time django starts it would run twice. It also runs when I migrate models, when I don't need it to.
I've read about the ready function https://docs.djangoproject.com/en/dev/ref/applications/#django.apps.AppConfig.ready, but cannot access the instantiated class outside of apps.py
Here is my current workflow:
in init.py:
from .my_module import ResourceHeavyClass
resource_heavy_instance = ResourceHeavyClass()
in my views.py
from . import resource_heavy_instance
This currently works, but I only want to load the module when the server starts, not when I make migrations. Appreciate any tips/advice.
You could make use of a SimpleLazyObject to postpone the creation until you really need it. Like for example:
from .my_module import ResourceHeavyClass
from django.utils.functional import SimpleLazyObject
class SomeClass:
resource_heave_instance = SimpleLazyObject(ResourceHeavyClass)
Now as long as you do not fetch the SomeClass.resource_heave_instance, it will not create the ResourceHeavyClass.
So if you for example have a method, you can use it like:
def some_method():
resource_heave_instance = SomeClass.resource_heave_instance
So here, when you call the some_method, it fetches the attribute, and it will indeed construct the object. But as long as the attribute is not fetched, it will not create a ResourceHeavyClass object. Once constructed, it will not create the object a second time.
So if the attribute is not fetched by just interpreting the file (so only by calling functions, and other continuations), we are safe.

Object has no attribute '__bases__' when calling inspect.getmro()

I have a python class that inherits from storm.py from the Apache Storm MultiLang project.
My class looks like the following:
import storm
class MyClassName(Storm.Bolt):
def initialize(self,conf,context):
self._conf = conf;
self._context = context
def process(self, in_tuple):
storm.ack(in_tuple)
if __name__ == '__main__':
MyClassName().run()
I copied my python file (myfilename.py) out to /usr/lib64/python2.7/site-package. I then logged into the python shell and did an import myfilename. That completed without error. When I run the following inspect.getmro(myfilename.MyClassName()) I get the following error:
AttributeError: 'MyClassName' object has no attribute '__bases__'
I was under the impression that when I declared my class and passed it Storm.Bolt that I was extending Storm.Bolt. My questions are:
Do I need to define __bases__ in my class?
What else am I missing?
Using Python 2.7.13 on CentOs7. Storm version is 1.1.0
The inspect.getmro function expects its argument to be a class, but you're passing it an instance. Get rid of the parentheses that call the class and your code should work:
inspect.getmro(myfilename.MyClassName) # not MyClassName()!
If the call you gave in the question was a simplified example and you don't have the class directly available where you're calling getmro on the instance, you can use type to get the class:
obj = SomeClass() # this happens somewhere earlier on, and we don't know SomeClass below
inspect.getmro(type(obj)) # but we can easily get it using type()

python 2.7 Use inside one function a method of a class, when class instance is in another function

This is a an example of the code:
module1.py is imported in the main.
In modul1.py, there is an init() function that creates classes from a previous imported library, and then, other functions uses this instance of the class, and the methods of that class.
ERROR: global name name1 not defined
module1.py:
from lib import class1, classs2
def init():
name1.class1()
def function():
name1.class1method1()
main.py:
import module1
init()
function()
I need some help, thanks
I think you may be getting confused between creating an object from a class definition and accessing the methods of the class. You are getting a not defined error because you have yet to define name1.
With the following adjustments, your code would work:
module1.py:
from lib import class1, classs2
def Init():
global name1
name1 = class1()
def function():
name1.class1method1()
main.py:
import module1
module1.Init()
module1.function()
That being said global variables are a bad idea, so the above code is for demonstration purposes only, not for actual use.

django import a view function

I have a django application xxx which does a number of things.
I also have a sepaerate application yyy. Which wants to call one of the functions of xxx.
Is there a way for me to import the functions?
For example, in yyy can i say
from toplevel.xxx import doit
Or what is the best approach, I dont want to duplicate code.
Of course, you can fo it.
With a proper import and parameter, you can do it.
#app: app1
#someview.py
def a_view(request, someparam):
#some code here
#app: app2
#otherview.py
from app1.someview import a_view
def another_view(request):
param = 1
a_view(request, param)
As for an example
UPDATE: Wish to mention that, your function a_view() do not have to get a parameter at all. So you can call functions with no paramaters. I just wish to mention that, if your function have paramaters, you have to pass them as if you do within an application.