Finding matching interval(s) in pandas Intervalindex

There’s this interesting API called Intervalindex new in 0.20 that lets you create an index of intervals. Given some sample data: data = [(893.1516130000001, 903.9187099999999), (882.384516, 893.1516130000001), (817.781935, 828.549032)] You can create the index like this: idx = pd.IntervalIndex.from_tuples(data) print(idx) IntervalIndex([(893.151613, 903.91871], (882.384516, 893.151613], (817.781935, 828.549032]] closed=’right’, dtype=’interval[float64]’) An interesting property of Intervals is that … Read more

Python elasticsearch.helpers.scan example

Can someone provide scan API example of python elasticsearch helpers client? res = elasticsearch.helpers.scan(….) How can i get all results from elasticsearch with res object? Answer The documentation includes an example, although if I’m reading it right, helpers.scan by default sets search_type=scan, which was removed in ES 5.1. This causes the example code to fail … Read more

How to test Django’s UpdateView?

As a simplified example, I’ve written an UpdateView for a Book model, as well as a ListView to redirect to upon success: from django.urls import reverse from django.views.generic import ListView from django.views.generic.edit import UpdateView from .models import Book class BookUpdate(UpdateView): model = Book fields = [‘title’, ‘author’] class BookList(ListView): model = Book The Book model … Read more

Convert WindowsPath to String

redpath = os.path.realpath(‘.’) thispath = os.path.realpath(redpath) fspec = glob.glob(redpath+’/*fits’) thispath = os.path.realpath(thispath+’/../../../..’) p = Path(thispath) userinput = ‘n’ while (userinput == ‘n’): text_file = next(p.glob(‘**/*.fits’)) print(“Is this the correct file path?”) print(text_file) userinput = input(“y or n”) parent_dir = text_file.parent.resolve() fspec = glob.glob(parent_dir+’/*fits’) I am getting the error unsupported operand type(s) for +: ‘WindowsPath’ and … Read more

How to solve CORS problem of my Django API?

I cannot solve CORS problem in my Django API. When I make a call to this API, I get error: Access to fetch at ‘http://localhost:8000/‘ from origin ‘http://localhost‘ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. If an opaque … Read more

Spacy nlp = spacy.load(“en_core_web_lg”)

I already have spaCy downloaded, but everytime I try the nlp = spacy.load(“en_core_web_lg”), command, I get this error: OSError: [E050] Can’t find model ‘en_core_web_lg’. It doesn’t seem to be a shortcut link, a Python package or a valid path to a data directory. I already tried >>> import spacy >>> nlp = spacy.load(“en_core_web_sm”) and this … Read more

__new__ method giving error object.__new__() takes exactly one argument (the type to instantiate)

why the following code is giving error? class Foo: def __new__(cls, *args, **kwargs): print(“Creating Instance”) instance = super(Foo, cls).__new__(cls,*args, **kwargs) return instance def __init__(self, a, b): self.a = a self.b = b z= Foo(2,3) it is giving the following error TypeError: object.__new__() takes exactly one argument (the type to instantiate) Answer instance = super(Foo, cls).__new__(cls,*args, … Read more

Change command Method for Tkinter Button in Python

I create a new Button object but did not specify the command option upon creation. Is there a way in Tkinter to change the command (onclick) function after the object has been created? Answer Though Eli Courtwright’s program will work fine¹, what you really seem to want though is just a way to reconfigure after … Read more