
    H&hx                     ,   d dl Z d dlZd dlZd dlZd dlmZ d dlmZ dZ	d Z
d Zd Zd Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zi fdZ d Z!d Z"d Z#d Z$dNd Z%d! Z&d" Z'd# Z(d$ Z)d% Z*d& Z+d' Z,d( Z-d) Z.d* Z/d+ Z0d, Z1d- Z2d. Z3d/ Z4d0 Z5d1 Z6d2 Z7d3 Z8d4 Z9d5 Z:d6 Z;d7 Z<dOd8Z=d9 Z>d: Z?d; Z@d< ZAd=d>iZBd?d@iZCdA ZDdB ZEdC ZFdPdDZGdPdEZHdPdFZIdG ZJdH ZKdI ZLdJ ZMdK ZNdL ZOePdMk(  rd dlQZQ eQj                          yy)Q    N)etree)Refactorz8http://www.itis.gov/ITISWebService/services/ITISService/c                       t        t        dz   i d      j                  d	i | }ddi}|j                  d|      }|D cg c]  }|j                   c}d   }|S c c}w )
z^
    Ping the ITIS API

    Usage::

        import pytaxize
        pytaxize.itis_ping()
    getDescriptiongetpayloadrequestax26z%http://itis_service.itis.usgs.gov/xsdz//ax26:description
namespacesr    r   	itis_basexmlxpathtext)kwargsttnsnodesxr   s         \/mounts/lovelace/software/anaconda3/envs/py312/lib/python3.12/site-packages/pytaxize/itis.py	itis_pingr   
   sk     
O)..E	J	N	N	XQW	XB
8	9BHH)bH9E!"qAFF"1%DK #s   Ac                     t        t        dz   d| id      j                  d
i |}|j                         }|d   j                         d   j	                         d   dk(  r"|d   j                         d   j
                  }|S |d   j                         d   j                         }t        |      }|j                  d       |d   j                         d   j
                  |d	<   |S )a5  
    Get accepted names from tsn

    :param tsn: taxonomic serial number (TSN) (character or numeric)

    Usage::

        # TSN accepted - good name
        pytaxize.getacceptednamesfromtsn('208527')
        # TSN not accepted - input TSN is old name
        pytaxize.getacceptednamesfromtsn('504239')
    getAcceptedNamesFromTSNtsnr   r   r      trueauthorsubmittedTsnr   )r   r   r   getchildrenvaluesr   _parse_nodespop)r   r   outtempdatr   s         r   getacceptednamesfromtsnr)      s     c(9885#,X]
^
b
b
lek
lC??DAwQ&&(+v51g!!#A&++ J	 Q##%a(4465!"1g113A6;;NJ    c                      t        t        dz   d| id      j                  di |}t        |j	                         d   j
                        S )a  
    Get any match count.

    :param x: text or taxonomic serial number (TSN) (character or numeric)
    :param **kwargs: Curl options passed on to `requests.get`

    Usage::

        pytaxize.getanymatchcount(x=202385)
        pytaxize.getanymatchcount(x="dolphin")
    getAnyMatchCountsrchKeyr   r   r   r   )r   r   r   intr"   r   )r   r   r&   s      r   getanymatchcountr/   1   sL     ^(911Iq>SX
Y
]
]
g`f
gCs #(())r*   c                      t        t        dz   d| id      j                  d	i |}ddi}g d}g d}t        ||||      S )
z
    Get comment detail from TSN

    :param tsn: TSN for a taxonomic group (numeric)
    :param **kwargs: Curl options passed on to `requests.get`

    Usage::

        pytaxize.getcommentdetailfromtsn(tsn=180543)
    getCommentDetailFromTSNr   r   r   ax21*http://data.itis_service.itis.usgs.gov/xsd)commentDetail	commentIdcommentTimeStampcommentator
updateDate)commentcommidcommtimer7   
updatedater   )r   r   r   	_itisdict)r   r   r&   r   matchescolnamess         r   getcommentdetailfromtsnr@   @   sU     c(9885#,X]
^
b
b
lek
lC
=	>B\GIHS"gx00r*   c           	           t        t        dz   d| id      j                  di |}ddi}g d}g d}t        ||||      }|d	   d
= t        D cg c]  }t        t        ||             c}S c c}w )z
    Get common names from tsn

    :param tsn: TSN for a taxonomic group (numeric)
    :param **kwargs: Curl options passed on to `requests.get`

    Usage::

        pytaxize.getcommonnamesfromtsn(tsn=183833)
    getCommonNamesFromTSNr   r   r   r2   r3   
commonNamelanguager   )comnamelangr      r   )r   r   r   _itisextractr   dictzip)r   r   r&   r   r>   r?   reszs           r   getcommonnamesfromtsnrO   Q   s     a(966V[
\
`
`
jci
jC
=	>B/G'H
sB
2CAr
-.0T#h"#000s   A/c                 z     t        t        dz   d| id      j                  di |}ddi}g d}t        |||      S )	z
    Get core metadata from tsn

    Usage::

        # coverage and currrency data
        pytaxize.getcoremetadatafromtsn(tsn=28727)
        # no coverage or currrency data
        pytaxize.getcoremetadatafromtsn(tsn=183671)
    getCoreMetadataFromTSNr   r   r   r2   r3   )
credRatingrankIdtaxonCoveragetaxonCurrencytaxonUsageRatingr   r   r   r   r   _itis_parser   r   r&   r   togets        r   getcoremetadatafromtsnr[   f   sN     b(977%W\
]
a
a
kdj
kC
=	>B\Euc2&&r*   c                      t        t        dz   d| id      j                  di |}g d}t        |t        |t        |            }|S )z
    Get coverge from tsn

    Usage::

        # coverage data
        pytaxize.getcoveragefromtsn(tsn=28727)
        # no coverage data
        pytaxize.getcoveragefromtsn(526852)
    getCoverageFromTSNr   r   r   )rS   rT   r   r   r   r   r   r=   ns21_tolowerr   r   r&   r>   dfs        r   getcoveragefromtsnrc   v   sN     ^(933eS\SX
Y
]
]
g`f
gC0G	3gx'8	9BIr*   c                      t        t        dz   d| id      j                  di |}ddg}t        |t        |t        |            }|S )z
    Get credibility rating from tsn

    Usage::

        pytaxize.getcredibilityratingfromtsn(526852)
        pytaxize.getcredibilityratingfromtsn(28727)
    getCredibilityRatingFromTSNr   r   r   rR   r   r^   ra   s        r   getcredibilityratingfromtsnrf      sS     g(9<<ucl\a
b
f
f
pio
pCU#G	3gx'8	9BIr*   c                       t        t        dz   i d      j                  di | }|j                  dt              }|D cg c]  }|j
                   c}S c c}w )z
    Get possible credibility ratings

    :param **kwargs: Curl options passed on to `requests.get`

    Usage::

        pytaxize.getcredibilityratings()
    getCredibilityRatingsr   r   z//ax23:credibilityValuesr   r   )r   r   r   r   ns23r   )r   r&   r   r   s       r   getcredibilityratingsrj      sU     W(966E
R
V
V
`Y_
`CII0TIBE!"qAFF"""   Ac                      t        t        dz   d| id      j                  di |}g d}t        |t        |t        |            }|S )z
    Get currency from tsn

    Usage::

        # currency data
        pytaxize.getcurrencyfromtsn(28727)
        # no currency dat
        pytaxize.getcurrencyfromtsn(526852)
    getCurrencyFromTSNr   r   r   )rS   rU   r   r   r^   ra   s        r   getcurrencyfromtsnrn      sN     ^(933eS\SX
Y
]
]
g`f
gC.G	3gx'8	9BIr*   c                      t        t        dz   d| id      j                  di |}g d}t        |t        |t        |            }|S )zZ
    Get date data from tsn

    Usage::

        pytaxize.getdatedatafromtsn(180543)
    getDateDataFromTSNr   r   r   )initialTimeStampr8   r   r   r^   ra   s        r   getdatedatafromtsnrr      sN     ^(933eS\SX
Y
]
]
g`f
gC5G	3gx'8	9BIr*   c                 z     t        t        dz   d| id      j                  di |}g d}t        ||t              S )zf
    Get expert information for the TSN.

    Usage::

        pytaxize.getexpertsfromtsn(180544)
    getExpertsFromTSNr   r   r   )r9   expertnamereferredTsnreferenceForr8   r   r   r   r   rX   r_   r   r   r&   rZ   s       r   getexpertsfromtsnr{      sC     ](922UCLRW
X
\
\
f_e
fCQEuc4((r*   c                 t     t        t        dz   d| id      j                  di |}ddi}t        ||      }|S )z
    Returns the kingdom and rank information for the TSN.

    :param tsn: TSN for a taxonomic group (numeric)

    Usage::

        pytaxize.gettaxonomicranknamefromtsn(tsn = 202385)
    getTaxonomicRankNameFromTSNr   r   r   r2   r3   r   r   r   r   	_parse2dfr   r   r   r   rb   s        r   gettaxonomicranknamefromtsnr      sK     
f);;eS\[`	a	e	e	ohn	oB
=	>B	2r	BIr*   c                 t     t        t        dz   d| id      j                  di |}t        |t              }|S )z
    Get full hierarchy from ts

    :param tsn: TSN for a taxonomic group (numeric)

    Usage::

        pytaxize.getfullhierarchyfromtsn(tsn = 37906)
        pytaxize.getfullhierarchyfromtsn(tsn = 100800)
    getFullHierarchyFromTSNr   r   r   r   )r   r   r   _parse_hierr_   )r   r   r   rb   s       r   getfullhierarchyfromtsnr      s@     
b)77%W\	]	a	a	kdj	kB	R	BIr*   c                      t        t        | z   |d      j                  di |g d}fd}|D cg c]
  } ||       c}S c c}w )Nr   r   )acceptedNameListcommentListcommonNameListcompletenessRatingcoreMetadatacredibilityRatingcurrencyRatingdateData
expertListgeographicDivisionListhierarchyUpjurisdictionalOriginListkingdomotherSourceList	parentTSNpublicationListscientificNamesynonymListtaxRanktaxonAuthorunacceptReasonusagec                 p    j                  d| z   t              d   j                         }t        |      S )N//ax21:r   r   )r   r_   r"   	_get_text)r   chr&   s     r   parsedatz_fullrecord.<locals>.parsedat   s1    YYy{tY4Q7CCE}r*   r   )r   r   r   )verbargsr   rZ   r   r   r&   s         @r   _fullrecordr      sM    
E(9t#T5
A
E
E
O
OCOE
 "''AHQK'''s   Ac                      t        dd| ifi |S )a6  
    Returns the full ITIS record for the TSN in the LSID, found by comparing the
    TSN in the search key to the TSN field. Returns an empty result set if
    there is no match or the TSN is invalid.

    :param lsid: lsid for a taxonomic group (character)
    :param **kwargs: Curl options passed on to `requests.get`

    Usage::

        pytaxize.getfullrecordfromlsid("urn:lsid:itis.gov:itis_tsn:180543")
        pytaxize.getfullrecordfromlsid("urn:lsid:itis.gov:itis_tsn:37906")
        pytaxize.getfullrecordfromlsid("urn:lsid:itis.gov:itis_tsn:100800")
    getFullRecordFromLSIDlsidr   )r   r   s     r   getfullrecordfromlsidr      s     .I&IIr*   c                      t        dd| ifi |S )aC  
    Returns the full ITIS record for a TSN

    :param tsn: tsn for a taxonomic group (character)
    :param **kwargs: Curl options passed on to `requests.get`

    Usage::

        pytaxize.getfullrecordfromtsn("504239")
        pytaxize.getfullrecordfromtsn("202385")
        pytaxize.getfullrecordfromtsn("183833")
    getFullRecordFromTSNr   r   )r   r   s     r   getfullrecordfromtsnr     s     -s|FvFFr*   c                 z     t        t        dz   d| id      j                  di |}ddg}t        ||t              S )zp
    Get geographic divisions from tsn

    Usage::

        pytaxize.getgeographicdivisionsfromtsn(180543)
    getGeographicDivisionsFromTSNr   r   r   geographicValuer8   r   ry   rz   s       r   getgeographicdivisionsfromtsnr     sI     i(9>>PS^c
d
h
h
rkq
rC|,Euc4((r*   c                       t        t        dz   i d      j                  di | }ddi}|j                  d|      }|D cg c]  }|j                   c}S c c}w )	z
    Get all possible geographic values

    :param **kwargs: Curl options passed on to `requests.get`

    Usage::

        pytaxize.getgeographicvalues()
    getGeographicValuesr   r   r2   .http://metadata.itis_service.itis.usgs.gov/xsdz//ax21:geographicValuesr   r   r   )r   r&   r   r   r   s        r   getgeographicvaluesr   #  s`     U(944b%
P
T
T
^W]
^C
A	BBII/BI?E!"qAFF"""rk   c                 z     t        t        dz   d| id      j                  di |}g d}t        ||t              S )z}
    Get global species completeness from tsn

    Usage::

        pytaxize.getglobalspeciescompletenessfromtsn(180541)
    #getGlobalSpeciesCompletenessFromTSNr   r   r   )completenessrS   r   r   ry   rz   s       r   #getglobalspeciescompletenessfromtsnr   2  sD     o(9DDuVYldi
j
n
n
xqw
xC+Euc4((r*   c                 t     t        t        dz   d| id      j                  di |}ddi}t        ||      }|S )z
    Get hierarchy down from tsn

    :param tsn: TSN for a taxonomic group (numeric)

    Usage::

        pytaxize.gethierarchydownfromtsn(tsn = 161030)
    getHierarchyDownFromTSNr   r   r   r2   r3   r   )r   r   r   r   r   s        r   gethierarchydownfromtsnr   ?  sK     
b)77%W\	]	a	a	kdj	kB
=	>B	R	BIr*   c                 t     t        t        dz   d| id      j                  di |}ddi}t        ||      }|S )z
    Get hierarchy up from tsn

    :param tsn: TSN for a taxonomic group (numeric)

    Usage::

        pytaxize.gethierarchyupfromtsn(tsn = 36485)
        pytaxize.gethierarchyupfromtsn(tsn = 37906)
    getHierarchyUpFromTSNr   r   r   r2   r3   r   r~   r   s        r   gethierarchyupfromtsnr   N  sK     
`)55s|UZ	[	_	_	ibh	iB
=	>B	2r	BIr*   c                     t        t        | z   |d      j                  di |}|j                  dt              }|D cg c]  }|j                          }}|D cg c]  }|D cg c]  }t        |       c} }}}g }	|D ]/  }|D cg c]  }t        |j                               d     }
}t        |
D cg c]  }|
j                  |      dkD  s| c}      }t        |      dkD  r|D ]  }dj                  |D cg c]>  }t        |j                               d   |k(  s#t        |j                               d   @ c}      }||i}|D cg c]#  }t        |j                               d   |vs"|% }}|j                  |        |	j                         |	j                  |       2 t        j                   |	D cg c]/  }|D ci c]  }|j#                         D ]  \  }}||
  c}}}1 c}}}}      }||d   D cg c]  }t        |j                               d     c}   S c c}w c c}w c c}}w c c}w c c}w c c}w c c}w c c}}}w c c}}}}w c c}w )	zI
    Get itis term

    Usage::

        pytaxize._itisterms("buya")
    r   r   z//ax21:itisTermsr   r   r   ,r   )r   r   r   r   r_   r"   _get_text_singlelistkeyssetcountlenjoinr#   appendpd	DataFrameitems)endptr   r   r&   r   r   nodes2yallnodesoutputkyzvnotuniqrN   mr   toadduuRdkrb   s                         r   
_itistermsr   ^  s    G(9u$dE
B
F
F
P
PCII(TI:E',-!ammo-F-:@AQa0!!$0AHAF *+,QtAFFH~a ,,#:Q1)9q:;w<! !XXQZ$qvvx.QRBSWXBXQXXZ 0 3Z[R	"#CQtAFFH~a'8'AqCC		% 	!
 MM"MM! 
FTTqBBAqwwyBtq!1a4B1BT	VBHQK9qaffh"9;;% .0A -: [C CT9sf   H=	I(I:I#I<I
I
$I(I#I9II("I!<I(#I0I!I(c                     | j                   g}| j                  j                  d      d   g}t        t	        ||            S N}r   r   tagsplitrK   rL   )r   valsr   s      r   r   r   }  s8    FF8DEEKKQ DD$  r*   c                 "    t        ddd| id|S )zl
    Get itis terms from common names

    Usage::

        pytaxize.getitistermsfromcommonname("buya")
    getITISTermsFromCommonNamer-   r   r   r   r   r   r   s     r   getitistermsfromcommonnamer     s     X8	1~XQWXXr*   c                 "    t        ddd| id|S )z\
    Get itis terms

    Usage::

        # fails
        pytaxize.getitisterms("bear")
    getITISTermsr-   r   r   r   r   s     r   getitistermsr     s     JN)QJ6JJr*   c                 "    t        ddd| id|S )z
    Get itis terms from scientific names

    Usage::

        pytaxize.getitistermsfromscientificname("ursidae")
        pytaxize.getitistermsfromscientificname("Ursus")
    getITISTermsFromScientificNamer-   r   r   r   r   s     r   getitistermsfromscientificnamer     s     \<Iq>\U[\\r*   c                    t        |       }g }|dk(  r8t        t        |            D ]  }|j                  t	        ||                ! |S |dk(  r8t        t        |            D ]  }|j                  t        ||                ! |S t        t        |            D ]  }|j                  t        ||                ! |S )a  
    Get hierarchies from TSN values, full, upstream only, or immediate downstream
    only. Uses the ITIS database.

    :param tsn: One or more TSN's (taxonomic serial number)
    :param what: One of full (full hierarchy), up (immediate upstream), or down
       (immediate downstream)

    Details Note that `pytaxize.itis_downstream` gets taxa downstream to a particular
       rank, whilc this function only gets immediate names downstream.

    Usage::

        # Get full hierarchy
        pytaxize.itis_hierarchy(tsn=180543)

        # Get hierarchy upstream
        pytaxize.itis_hierarchy(tsn=180543, "up")

        # Get hierarchy downstream
        pytaxize.itis_hierarchy(tsn=180543, "down")

        # Many tsn's
        pytaxize.itis_hierarchy(tsn=[180543,41074,36616])
    fullup)convertsingleranger   r   r   r   r   )r   whattsn2r'   is        r   itis_hierarchyr     s    4 DDv~s4y! 	:AKK/Q89	: K 
s4y! 	8AKK-d1g67	8
 K s4y! 	:AKK/Q89	:Kr*   c                 z     t        t        dz   d| id      j                  di |}ddi}g d}t        |||      S )	zr
    Get jurisdictional origin from tsn

    Usage::

        pytaxize.getjurisdictionaloriginfromtsn(180543)
    getJurisdictionalOriginFromTSNr   r   r   r2   r3   )jurisdictionValueoriginr8   r   rW   rY   s        r   getjurisdictionaloriginfromtsnr     sO     
i)>>PS^c	d	h	h	rkq	rC
=	>B7Euc2&&r*   c                  z     t        t        dz   i d      j                  di | }ddi}ddg}t        ||||d      S )	ze
    Get jurisdiction origin values

    Usage::

        pytaxize.getjurisdictionoriginvalues()
    getJurisdictionalOriginValuesr   r   ax23r   jurisdictionr   r   )r   r   r   _itisdfr   r&   r   r>   s       r   getjurisdictionoriginvaluesr    sS     
^)==rSX	Y	]	]	g`f	gC
A	BBh'G3GWf55r*   c                       t        t        dz   i d      j                  di | }|j                         d   j                         D cg c]  }|j                   }}t        j                  |dg      S c c}w )za
    Get possible jurisdiction values

    Usage::

        pytaxize.getjurisdictionvalues()
    getJurisdictionValuesr   r   r   jurisdictionValuescolumnsr   )r   r   r   r"   r   r   r   )r   r&   r   r   s       r   getjurisdictionvaluesr    sq     
V)55r5	Q	U	U	_X^	_C __.q1==?AQVVADA<<)=(>?? Bs   	A6c                     t        dd| ifi |} t        t        dz   d| id      j                  di |}ddi}g d}t	        |||      S )	za
    Get kingdom names from tsn

    Usage::

        pytaxize.getkingdomnamefromtsn(202385)
    getKingdomNameFromTSNr   r   r   r2   r3   	kingdomIdkingdomNamer   r   )_itisGETr   r   r   rX   rY   s        r   getkingdomnamefromtsnr    sc     *UCL
CF
CC	^)55c{TY	Z	^	^	hag	hC
=	>B-Euc2&&r*   c                       t        t        dz   i d      j                  di | }ddi}g d}t        |||t	        |      d      S )zY
    Get all possible kingdom names

    Usage::

        pytaxize.getkingdomnames()
    getKingdomNamesr   r   r   r   r  r   )r   r   r   r   r`   r  s       r   getkingdomnamesr    sR     
P)//U	K	O	O	YRX	YC
A	BB/G3GXg%6??r*   c                       t        t        dz   i d      j                  d
i | }ddi}|j                  d|      }|d   j                  }t        j                  |j                         d   d	      }|S )zr
    Provides the date the ITIS database was last updated.

    Usage::

        pytaxize.getlastchangedate()
    getLastChangeDater   r   r   r   z//ax23:updateDater   r   z%Y-%m-%dr   )r   r   r   r   r   timestrptimer   )r   r&   r   r   bbdts         r   getlastchangedater  
  st     
R)112u	M	Q	Q	[TZ	[C
A	BBII)bI9E	qB	rxxz!}j	1BIr*   c                     t        dd| ifi |} t        t        dz   d| id      j                  di |}|j	                         d   j
                  }|d}|S 	 |S )z
    Gets the unique LSID for the TSN, or an empty result if there is no match.

    Usage::

        # valid TSN
        pytaxize.getlsidfromtsn(155166)
        # invalid TSN, returns nothing
        pytaxize.getlsidfromtsn(0)
    getLSIDFromTSNr   r   r   r   no matchr   )r  r   r   r   r"   r   )r   r   r&   r   s       r   getlsidfromtsnr    su     #eS\
<V
<C	W)..sU	S	W	W	aZ`	aC		1		"	"B	z I 	Ir*   c                 z     t        t        dz   d| id      j                  di |}g d}t        ||t              S )z}
    Returns a list of the other sources used for the TSN.

    Usage::

        pytaxize.getothersourcesfromtsn(182662)
    getOtherSourcesFromTSNr   r   r   )acquisitionDaterv   rw   source
sourceTyper8   versionr   )r   r   r   _itis_parse_2dictr_   rz   s       r   getothersourcesfromtsnr%  -  sE     
`)66sUZ	[	_	_	ibh	iC-EUC..r*   c                 z     t        t        dz   d| id      j                  di |}ddg}t        ||t              S )zp
    Returns the parent TSN for the entered TSN.

    Usage::

        pytaxize.getparenttsnfromtsn(202385)
    getParentTSNFromTSNr   r   r   	parentTsnr   ry   rz   s       r   getparenttsnfromtsnr)  :  sH     
])33eC[RW	X	\	\	f_e	fCEuc4((r*   c                 z     t        t        dz   d| id      j                  di |}g d}t        ||t              S )zz
    Returns a list of the pulications used for the TSN.

    Usage::

        pytaxize.getpublicationsfromtsn(70340)
    getPublicationsFromTSNr   r   r   )actualPubDateisbnissnlistedPubDatepages
pubCommentpubNamepubPlace	publisherreferenceAuthorrv   refLanguagerw   titler8   r   ry   rz   s       r   getpublicationsfromtsnr8  F  sH     
`)66sUZ	[	_	_	ibh	iCIE uc4((r*   c                       t        t        dz   i d      j                  di | }g d}t        |t        |t        |      d      S )z
    Provides a list of all the unique rank names contained in the database and
    their kingdom and rank ID values.

    Usage::

        pytaxize.getranknames()
    getRankNamesr   r   )r  rS   rankNamer   r   r   r   r   r   ri   r`   r   r&   r>   s      r   getranknamesr>  T  sE     
M)n,b%	H	L	L	Vv	VC1G3gx'8&AAr*   c                 z     t        t        dz   d| id      j                  di |}g d}t        ||t              S )a$  
    Gets the partial ITIS record for the TSN in the LSID, found by comparing the
    TSN in the search key to the TSN field. Returns an empty result set if
    there is no match or the TSN is invalid.

    Usage::

        pytaxize.getrecordfromlsid("urn:lsid:itis.gov:itis_tsn:180543")
    getRecordFromLSIDr   r   r   )
authorship	genusPartinfragenericEpithetinfraspecificEpithetr   nameCompletenomenclaturalCoderank
rankStringspecificEpithet	uninomialr   r   ry   )r   r   r&   rZ   s       r   getrecordfromlsidrK  a  sH     
^)11FD>SX	Y	]	]	g`f	gCEE uc4((r*   c                 z     t        t        dz   d| id      j                  di |}g d}t        ||t              S )zj
    Returns the review year for the TSN.

    Usage::

        pytaxize.getreviewyearfromtsn(180541)
    getReviewYearFromTSNr   r   r   )rS   
reviewYearr   r   ry   rz   s       r   getreviewyearfromtsnrO  q  sC     
_)44uclTY	Z	^	^	hag	hC)Euc4((r*   c                 z     t        t        dz   d| id      j                  di |}g d}t        ||t              S )z
    Returns the scientific name for the TSN. Also returns the component parts
    (names and indicators) of the scientific name.

    Usage::

        pytaxize.getscientificnamefromtsn(531894)
    getScientificNameFromTSNr   r   r   )combinedNameunitInd1unitInd3	unitName1	unitName2	unitName3r   r   ry   rz   s       r   getscientificnamefromtsnrX  }  sE     
c)885#,X]	^	b	b	lek	lCEuc4((r*   c                 z     t        t        dz   d| id      j                  di |}g d}t        ||t              S )zv
    Returns the author information for the TSN.

    Usage::

        pytaxize.gettaxonauthorshipfromtsn(183671)
    getTaxonAuthorshipFromTSNr   r   r   )rA  r8   r   r   ry   rz   s       r   gettaxonauthorshipfromtsnr[    sC     
d)99E3<Y^	_	c	c	mfl	mC-Euc4((r*   c                 z     t        t        dz   d| id      j                  di |}ddg}t        ||t              S )zt
    Returns the usage information for the TSN.

    Usage::

        pytaxize.gettaxonomicusagefromtsn(526852)
    getTaxonomicUsageFromTSNr   r   r   rV   r   ry   rz   s       r   gettaxonomicusagefromtsnr^    sH     
c)885#,X]	^	b	b	lek	lC&Euc4((r*   c                      t        t        dz   d| id      j                  di |}g d}t        |t        |t        |            S )z
    Get tsn by vernacular language not the international language code (character)

    Usage::

        pytaxize.gettsnbyvernacularlanguage("french")
    getTsnByVernacularLanguagerE   r   r   rC   r   r   r   r   r   r_   r`   )rE   r   r&   r>   s       r   gettsnbyvernacularlanguagerb    sL     
o)::ZQYDZdi	j	n	n	xqw	xC-G3gx'899r*   c                      t        t        dz   d| id      j                  di |}|j                         d   j                  }|d}|S 	 |S )z
    Gets the TSN corresponding to the LSID, or an empty result if there is no match.

    Usage::

        pytaxize.gettsnfromlsid(lsid="urn:lsid:itis.gov:itis_tsn:28726")
        pytaxize.gettsnfromlsid("urn:lsid:itis.gov:itis_tsn:0")
    getTSNFromLSIDr   r   r   r   r  r   )r   r   r   r"   r   )r   r   r&   r   s       r   gettsnfromlsidre    sa     
[)..PU	V	Z	Z	d]c	dC		1		"	"B	z I 	Ir*   c                 z     t        t        dz   d| id      j                  di |}ddg}t        ||t              S )z
    Returns the unacceptability reason, if any, for the TSN.

    Usage::

        pytaxize.getunacceptabilityreasonfromtsn(183671)
    getUnacceptabilityReasonFromTSNr   r   r   r   r   ry   rz   s       r   getunacceptabilityreasonfromtsnrh    sJ     
j)??%QT_d	e	i	i	slr	sC#$Euc4((r*   c                       t        t        dz   i d      j                  di | }dg}t        |t        |t        |      d      S )z
    Provides a list of the unique languages used in the vernacular table.

    Usage::

        pytaxize.getvernacularlanguages()
    getVernacularLanguagesr   r   languageNamesr   r   r<  r=  s      r   getvernacularlanguagesrl    sJ     
W)66E	R	V	V	`Y_	`CG3gx'8&AAr*   c                      t        t        dz   d| id      j                  d	i |}g d}|j                  dt              }t        |d   t        |t        |            S )
z
    Search for tsn by common name

    Usage::

        pytaxize.searchbycommonname(x="american bullfrog")
        pytaxize.searchbycommonname("ferret-badger")
        pytaxize.searchbycommonname("polar bear")
    searchByCommonNamer-   r   r   rC   //ax21:commonNamesr   r   r   r   r   r   r   r_   r   r`   r   r   r&   r>   tmps        r   searchbycommonnamers    sa     
^)22YqMSX	Y	]	]	g`f	gC-G
))(T)
:C3q64(7*;<<r*   c                      t        t        dz   d| id      j                  d	i |}g d}|j                  dt              }t        |d   t        |t        |            S )
zz
    Search for tsn by common name beginning with

    Usage::

        pytaxize.searchbycommonnamebeginswith("inch")
    searchByCommonNameBeginsWithr-   r   r   rC   ro  r   r   r   rp  rq  s        r   searchbycommonnamebeginswithrv    sb     
h)<<yQRm]b	c	g	g	qjp	qC-G
))(T)
:C3q64(7*;<<r*   c                      t        t        dz   d| id      j                  d	i |}g d}|j                  dt              }t        |d   t        |t        |            S )
zv
    Search for tsn by common name ending with

    Usage::

        pytaxize.searchbycommonnameendswith("snake")
    searchByCommonNameEndsWithr-   r   r   rC   ro  r   r   r   rp  rq  s        r   searchbycommonnameendswithry    sa     
f)::YqM[`	a	e	e	ohn	oC-G
))(T)
:C3q64(7*;<<r*   c                 <    |dk(  rt        | fi |S t        | fi |S )a  
    Searches common name and acts as thin wrapper around
    `pytaxize.searchbycommonnamebeginswith` and `pytaxize.searchbycommonnameendswith`

    Usage::

        pytaxize.itis_searchcommon("inch")
        pytaxize.itis_searchcommon("inch", which = "end")
    begin)rv  ry  )r   whichr   s      r   itis_searchcommonr}    s,     +A888)!6v66r*   c                      t        t        dz   d| id      j                  di |}ddg}t        |t        |t        |            S )z
    Search by scientific name

    Usage::

        pytaxize.searchbyscientificname(x="Tardigrada")
        pytaxize.searchbyscientificname("Quercus douglasii")
    searchByScientificNamer-   r   r   rR  r   r   ra  )r   r   r&   r>   s       r   searchbyscientificnamer  '  sO     
b)661W\	]	a	a	kdj	kCe$G3gx'899r*   c                     t        t        dz   d| id      j                  di |}|j                         d   j                         }g }|D ]  }|j                         }|D ]q  }|j	                  t        t        |j                         D cg c]  }t        |       c}|j                         D cg c]  }|j                   c}                   s  |S c c}w c c}w )z
    Search for any match

    Usage::

        pytaxize.searchforanymatch(x=202385)
        pytaxize.searchforanymatch(x="dolphin")
    searchForAnyMatchr-   r   r   r   r   
r   r   r   r"   r   rK   rL   itergettagr   )r   r   r&   rr  r   r   wes           r   searchforanymatchr  4  s     
])11Ia=RW	X	\	\	f_e	fC
//
A

*
*
,CF `mmo 	`AMM$sqvvx#@!F1I#@STSYSYS[B\a166B\]^_	`` M $AB\s   C2Cc                    | |||d} t        t        dz   |d      j                  di |}|j                         d   j                         }g }|D ]  }	|	j                         }|D ]q  }
|j	                  t        t        |
j                         D cg c]  }t        |       c}|
j                         D cg c]  }|j                   c}                   s  |S c c}w c c}w )z
    Search for any matched page for descending (logical)

    Usage::

        pytaxize.searchforanymatchpaged(x=202385, pagesize=100, pagenum=1, ascend=False)
        pytaxize.searchforanymatchpaged("Zy", pagesize=100, pagenum=1, ascend=False)
    )r-   pageSizepageNumascendsearchForAnyMatchPagedr   r   r   r   r  )r   pagesizepagenumr  r   r   r&   rr  r   r   r  r  s               r   searchforanymatchpagedr  G  s     H&QD	X)66e	T	X	X	b[a	bC
//
A

*
*
,CF `mmo 	`AMM$sqvvx#@!F1I#@STSYSYS[B\a166B\]^_	`` M $AB\s   C7C$c                 >    | j                   j                  dk(  r| gS | S )Nr.   )	__class____name__)r   s    r   r   r   [  s     {{u$s
r*   r2   r3   r   r   c                     | j                  d|      }|D cg c]  }|j                   }}|D cg c]   }|j                  j                  d      d   " }}t	        t        ||            g}|S c c}w c c}w )Nz//ax21:*r   r   r   )r   r   r   r   rK   rL   )objr   r   r   r   r   rb   s          r   r   r   e  sp    IIjRI0E!"qAFF"D")./AAEEKKQ/D/
s4
	 BI #/s
   A0%A5c                     | D cg c]  }|j                    }}| D cg c]   }|j                  j                  d      d   " }}t        t	        ||            S c c}w c c}w r   r   )r  r   r   r   s       r   r$   r$   l  sW     qAFF D ),-AAEEKKQ-D-D$   !-
   A%Ac                     | j                  d|      }g }t        t        |            D ]C  }|j                  t	        t        g d||   D cg c]  }|j                   c}                   E |S c c}w )Nz//ax21:hierarchyListr   )r   r    
parentNamer(  r;  	taxonName)r   r   r   r   rK   rL   r   )r  r   r   r   r   r   s         r   r   r   q  ss    II,I<E	B3u: {
		$s[nstunv]wij^_^d^d]wxyz{I ^xs   A2c                    d|z  }|D cg c]  }||z   	 }}g }|D ]>  }| j                  ||      }	|j                  |	D cg c]  }|j                   c}       @ t        	      dk(  rt	        j
                  d       t        |d         t        |d         k(  s|d   dd  |d<   t        j                  t        t        |       |      |d d d      }
|
S c c}w c c}w )N//%s:r   r    Please enter a valid search namerI   r   r  )
r   r   r   r   sysexitr   r   r   rL   )abr>   r?   pastensprefixr   r   r   r   rb   s              r   r   r   x  s    wF!()Avax)G)F /a(u-!qvv-./ 5zQ34q	sF2J00 BZ^r
	d3<((	<httn	NBI * .s   CC
c                 >   d|z  }|D cg c]  }||z   	 }}g }|D ]>  }| j                  ||      }	|j                  |	D cg c]  }|j                   c}       @ t        	      dk(  rt	        j
                  d       t        t        ||            S c c}w c c}w )Nr  r   r   r  )r   r   r   r   r  r  rK   rL   
r  r  r>   r?   r  r  r   r   r   r   s
             r   r=   r=     s    wF!()Avax)G)F /a(u-!qvv-./ 5zQ34Hf%&& * .s   BB
c                     d|z  }|D cg c]  }||z   	 }}g }|D ]>  }| j                  ||      }	|j                  |	D cg c]  }|j                   c}       @ |S c c}w c c}w )Nr  r   )r   r   r   r  s
             r   rJ   rJ     su    wF!()Avax)G)F /a(u-!qvv-./ M * .s   AA$
c                     t        | D cg c]  }t        |      dk(   c}      r*t        j                  t	        t        ||                   }|S t        j                  t	        t        ||             g      }|S c c}w )NrH   )allr   r   r   rK   rL   )r  r?   r   rb   s       r   	_array2dfr    sg    
s#!CFAI#$\\$s8S123 I \\4Hc 2345I	 $s   A;c                 |    d }| D cg c]  } ||||       }}t        t        t        |       |            S c c}w )Nc                 l    |j                  d| z   |      }|D  cg c]  } | j                   c} S c c} w Nr   r   r   r   r   r   nsprr  s       r   	xpathfuncz_itis_parse.<locals>.xpathfunc  0    ggikcg2 #$1$$$   1)rK   rL   r`   r  r  r   r  r   r   s         r   rX   rX     s@    % )**1IaA*D*HQK&'' +s   9c                 j    d }| D cg c]  } ||||       }}t        t        | |            S c c}w )Nc                 l    |j                  d| z   |      }|D  cg c]  } | j                   c} S c c} w r  r  r  s       r   r  z$_itis_parse_2dict.<locals>.xpathfunc  r  r  )rK   rL   r  s         r   r$  r$    s;    % )**1IaA*D*At +s   0c                     | D cg c]  }|j                    }}| D cg c]   }|j                  j                  d      d   " }}t        t	        ||            S c c}w c c}w r   r   )r   r   r   r   s       r   r   r     sW    qAFFD)*+AAEEKKQ+D+D$   +r  c                 H    | D cg c]  }|j                          c}S c c}w )N)lower)r   r   s     r   r`   r`     s     !!AGGI!!!s   c                 >    | j                   j                  d      d   S r   )r   r   )r   s    r   r  r    s    55;;sAr*   __main__)Nr   )r{  )r2   )Sr  r  requestspandasr   lxmlr   pytaxize.refactorr   r   r   r)   r/   r@   rO   r[   rc   rf   rj   rn   rr   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r%  r)  r8  r>  rK  rO  rX  r[  r^  rb  re  rh  rl  rs  rv  ry  r}  r  r  r  r   r_   ri   r   r$   r   r   r=   rJ   r  rX   r$  r   r`   r  r  doctesttestmodr   r*   r   <module>r     s   
     &F	0*1"1*'  #  
)
(J"G
)#
)   <>!
Y	K	]%N'6
@'@(/
))B) 
))J
)
)
:"
)
B===7:&( 	;<?@!
 	'(!
" zGOO r*   