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:

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)+$")

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

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

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

# a sample of 5 but realisticlly - thousands?
DOIs = (

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

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


  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:
        url = requests.get(f'**{identificador}**/children?ca_deviceTypes=null%7C401&fields=pid,title,seasonNumber,childrenCount,commercializationType,availableUntil&includeRelations=Episode,Media&includeAttributes=ca_devicetypes_qualities&contentPid=**{identificador}**&cachetime=202103041800')
          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?

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.


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”)



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”, 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 ?


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

    import pandas as pd



    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:
    if ii*jj not in list1(1::2):
    elif ii+jj in list1(::2):
    if ii==0:
    elif jj==1:
    elif ii==3:
    for label , color in zip(range(len(iris.target_names)),colors):
              , label=iris.target_names(label)
              , color=color)

    ax(b,a).legend(loc="upper right")


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]

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

[Property file]


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