python – DOI name validation with pydantic

I’m trying to understand how to use pydantic for data parsing & validation.

The idea I had in mind was following:

  • I have a list of DOI names for which I need to collect metadata
  • I could use pydantic to test if the DOI name is valid
    • if it’s valid, I proceed with making a request to scite api
    • if not, the code raises an exception instead of calling api

Very basic idea as you see.


The code:

scite_utils.py

import requests
from ratelimit import limits, sleep_and_retry
from pydantic import BaseModel, ValidationError, constr


class DOI(BaseModel):
    name: constr(regex=r"^10.d{4,9}/(-._;()/:a-zA-Z0-9)+$")


@sleep_and_retry
@limits(calls=40, period=60) # api ratelimits
def remote_call(endpoint: str):
    r = requests.get(f"https://api.scite.ai/{endpoint}")
    r.encoding = "UTF-8"
    return r.json() 


def get_paper(doi: DOI):
    if not isinstance(doi, DOI):
        try:
            doi = DOI(name=doi).name
        except ValidationError:
            return 
    return remote_call(endpoint=f"papers/{doi}")

test.py

import pandas as pd
from typing import List
from scite_utils import DOI, get_paper


# a sample of 5 but realisticlly - thousands?
DOIs = (
    "10.12724/ajss.47.0", 
    "10.31124/advance.12630065.v1", 
    "obv_error",
    "10.1122ajss.123.9",
    "10.1080/09662830500528294"
)


def create_table(names: List(DOI)):
    data = (get_paper(name) for name in names)
    return pd.DataFrame(
        publication 
        for publication in data
        if publication is not None
    )

if __name__=="__main__":
    result = create_table(DOIs)
    print(result)

Questions:

  1. Is this a valid use case for pydantic?
  2. Should I validate DOI name inside of the function that makes a request
    (or is there a better place for it, like a separate function)?
  3. Doesn’t it look awkward generally? I feel like I’m missing the point,
    because I could’ve written a custom function with re to validate DOI name.

json – String multi linea con format en python, como declararlo de la manera mas prolija posible?

Tengo una url la cual debo pasarle distintos id por parametros:

ids = ('MOV42425', 'MOV28393', 'MOV99345', etc)
for identificador in ids:
    try:
        url = requests.get(f'https://contentapi-ar.cdn.telefonica.com/29/default/es-AR/content/**{identificador}**/children?ca_deviceTypes=null%7C401&fields=pid,title,seasonNumber,childrenCount,commercializationType,availableUntil&includeRelations=Episode,Media&includeAttributes=ca_devicetypes_qualities&contentPid=**{identificador}**&cachetime=202103041800')
    except:
          print(f'Error de conexión con url id {identificador}')

Quisiera que ocupe varias lineas porque queda bastante ilegible a lo largo, como es posible hacerlo teniendo el string format dentro?
Gracias!

matplotlib – Implementation of a contour plot [Python]

I am trying to make a contour plot using meshgrid with Python. My dependent variable is not a direct function of the two, but I enter the values by hand. Consider the following example code:

import numpy as np
import matplotlib.pyplot as plt

Array1 = np.arange(-5,5,0.5)
Array2 = np.arange(-5,5,0.5)

X, Y = np.meshgrid(Array1, Array2)

Z = np.ones(X.shape)
Z((1,2)) = 3
Z((1,5)) = 8
Z((3,4)) = -3

plt.contour(X, Y, Z, 500)

This creates a contour plot that spans X values between -5 and 5, but Y values only between -5 and -2. Why does this happen, that part of the plot stays blank without contour lines. I expected lines to cover the whole plane, for -5 <X <5 and for -5 < Y < 5.

Thanks.

How do I select specific elements from an array in python?

I have multiple arrays in python in the following form

array1=['8', '-9.7521970601311E-5', '1237.16', '1265.50"', 'spec2', '=', '"2', '59', '0', '9810.07016902']

I want to select the data point that ends with E-some number. In the case of array1 that data point is -9.7521970601311E-5.

The arrays are not symmetric and the data points with the number ending in E are quite random. The length of the arrays are however the same. I wanted to create a loop where it would look through each array to find out the number ending with E and store them in a separate array. How would I go about doing that? I am quite new to python and I have no idea how to even approach this problem.

python – Alguém saberia me explicar pq apareceu isso”Exception has occurred: ImportError Missing optional dependency ‘xlrd’ “?

Estou utilizando o vscode para usar o pandas em python
Meu código

”’
from IPython.display import display

import pandas as pd

df = pd.read_excel(r”Vendas.xlsx”)

display(df)

”’

e me apareceu esta informação na linha do “df = …”

Exception has occurred: ImportError
Missing optional dependency ‘xlrd’. Install xlrd >= 1.0.0 for Excel support Use pip or conda to install xlrd.
File “C:UsersDellOneDriveDocumentsProjetos c_py_luaProjetos PYCurso pythonIntensivão Python 1vendas.py”, line 4, in
df = pd.read_excel(r”Vendas.xlsx”)

alguém poderia me ajudar nessa do pq não estou conseguindo ler esse arquivo .xlsx?

python – scatter subplot for iris dataset

I’m new to data science. I wrote this script for plotting all different kinds of iris data set scatter plot. trying not to plot something with itself . how can I optimize my code ?

”’python

    from sklearn.datasets import load_iris
    import matplotlib.pyplot as plt
    import numpy as np

    import pandas as pd

    iris=load_iris()

    list1=()

    fig, ax =plt.subplots(nrows=3,ncols=2,figsize=(10,10))

for ii in range(4):
  for jj in range(1,4):
    if ii==jj:
      break
    if ii*jj not in list1(1::2):
      list1.extend((ii+jj,ii*jj))
    elif ii+jj in list1(::2):
      break
    a=ii
    b=jj
    x_index=ii
    y_index=jj
    colors=('blue','red','green')
    if ii==0:
      b=b-1
    elif jj==1:
      a=a-2
      b,a=a,b
    elif ii==3:
      a=a-1
      b=b-1
      a,b=b,a
    for label , color in zip(range(len(iris.target_names)),colors):
      ax(b,a).scatter(iris.data(iris.target==label,x_index) 
              , iris.data(iris.target==label,y_index)
              , label=iris.target_names(label)
              , color=color)

    ax(b,a).set_xlabel(iris.feature_names(x_index))
    ax(b,a).set_ylabel(iris.feature_names(y_index))
    ax(b,a).legend(loc="upper right")
    fig.tight_layout()
    fig.show()

”’

enter image description here
this is the output

how would you write it if it was you?

I appreciate any help.

python – Como posso carregar arquivos de forma que eles funcionem em outros PCs mesmo que o caminho até o arquivo seja diferente? (Pygame)

Opa! Então… Eu estou fazendo um programa usando o pygame, e na hora de carregar um arquivo, eu devo usar isso, certo?

bulletImg = pygame.image.load('C:UsersTarcísioPycharmProjectspyGameSpIimgsbullet.png')

Mas isso só vai funcionar no meu computador, e não no computador de outras pessoas, porque o caminho até o arquivo é diferente. O que devo fazer?

python 3.x – problema con PhotImage en tkinter de python3

pues resulta que estoy conociendo el úso de canvas y cuando quiero insertar una imágen al widget ocúrre esto :

introducir la descripción de la imagen aquí

éste el el código que estoy practicando:

import tkinter

top = tkinter.Toplevel ()

can = tkinter.Canvas (top , bg =”gray” , height =500 , width =500)
image_1 = tkinter.PhotoImage (file =” “)

image = can.create_image (200 , 200 , image = image_1)

can.pack ()

top.mainloop ()

After removing text from file, how to remove empty lines from file using python

I am removing content from file and rewriting using python
This is the initial script

[Property file]
username=admin
pwd=admin
host1=0.0.0.0
host2=1.1.1.1

I am changing host values.After changing through python script script, white spaces are creating

[Property file]
username=admin
pwd=admin


host1=0.0.0.0
host2=1.1.1.1

Everytime I run script it creating more spaces.Can anyone suggest an idea. Thanks in advance